世界上最伟大的投资就是投资自己的教育
import chai, { expect } from "chai";
import { app, authToken } from "./helpers/tests-helper";
import { UNAUTHORIZED, OK, UNPROCESSABLE_ENTITY } from "http-status-codes";
const postToTest = {
body: "body"
};
let resPost: any;
before(async () => {
resPost = await chai
.request(app)
.post("/api/posts")
.set("Authorization", authToken)
.send(postToTest);
});
describe("Read Post", () => {
describe("GET /api/posts", () => {
it("显示所有的 posts", async () => {
const res = await chai.request(app).get("/api/posts");
expect(res).to.have.status(OK);
expect(res.body.success).to.equal(true);
expect(res.body.data.docs).to.have.lengthOf(1);
expect(res.body.data).to.have.property("page");
});
it("分页", async () => {
const res = await chai.request(app).get("/api/posts?page=2");
expect(res).to.have.status(OK);
expect(res.body.success).to.equal(true);
expect(res.body.data).to.have.property("docs");
expect(res.body.data).to.have.property("page");
expect(res.body.data.page).to.equal("2");
});
});
describe("GET /api/posts/:id", () => {
it("显示单个 post", async () => {
const res = await chai
.request(app)
.get(`/api/posts/${resPost.body.data.post._id}`);
expect(res).to.have.status(OK);
expect(res.body.success).to.equal(true);
expect(res.body.data).to.have.property("post");
});
});
});
describe("Create Post", () => {
describe("POST /api/posts", () => {
context("如果没有登录时", () => {
it("不能创建 Post", async () => {
const res = await chai
.request(app)
.post("/api/posts")
.send(postToTest);
expect(res).to.have.status(UNAUTHORIZED);
expect(res.body.success).to.equal(false);
expect(res.body).to.have.property("message");
});
});
context("如果有登录时", () => {
it("没有填写 body 不能创建 Post", async () => {
const res = await chai
.request(app)
.post("/api/posts")
.set("Authorization", authToken)
.send({ body: "" });
expect(res).to.have.status(UNPROCESSABLE_ENTITY);
expect(res.body.success).to.equal(false);
expect(res.body.errors).to.have.property("body");
});
it("填写了 body 可以创建 Post", async () => {
const res = await chai
.request(app)
.post("/api/posts")
.set("Authorization", authToken)
.send(postToTest);
expect(res).to.have.status(OK);
expect(res.body.success).to.equal(true);
expect(res.body.data).to.have.property("message");
expect(res.body.data).to.have.property("post");
});
});
});
});
describe("Update Post", () => {
describe("PUT /api/posts/:id", () => {
context("如果没有登录时", () => {
it("不能更新 Post", async () => {
const res = await chai
.request(app)
.put(`/api/posts/${resPost.body.data.post._id}`)
.send(postToTest);
expect(res).to.have.status(UNAUTHORIZED);
expect(res.body.success).to.equal(false);
expect(res.body).to.have.property("message");
});
});
context("如果有登录时", () => {
it("body 为空不能更新 Post", async () => {
const res = await chai
.request(app)
.put(`/api/posts/${resPost.body.data.post._id}`)
.set("Authorization", authToken)
.send({ body: "" });
expect(res).to.have.status(UNPROCESSABLE_ENTITY);
expect(res.body.success).to.equal(false);
expect(res.body.errors).to.have.property("body");
});
it("填写了 body 可以更新 Post", async () => {
const res = await chai
.request(app)
.put(`/api/posts/${resPost.body.data.post._id}`)
.set("Authorization", authToken)
.send({ body: "newBody" });
expect(res).to.have.status(OK);
expect(res.body.success).to.equal(true);
expect(res.body.data).to.have.property("message");
expect(res.body.data).to.have.property("post");
expect(res.body.data.post.body).to.equal("newBody");
});
});
});
});
04:251Free使用 TypeScript & mocha & chai 写测试代码实战视频教程 #1 课程介绍
08:312Free使用 TypeScript & mocha & chai 写测试代码实战视频教程 #2 搭建测试环境
15:043Pro使用 TypeScript & mocha & chai 写测试代码实战视频教程 #3 写第一个测试
10:184Pro使用 TypeScript & mocha & chai 写测试代码实战 #4 每次测试完应该把数据库清空
07:255Pro使用 TypeScript & mocha & chai 写测试代码实战视频教程 #5 完成注册功能的测试
05:076Pro使用 TypeScript & mocha & chai 写测试代码实战视频教程 #6 完成登录功能测试
11:057Pro使用 TypeScript & mocha & chai 写测试代码实战 #7 jwt token 自动登录的处理 - 创建 Post 前要先登录
07:348Pro使用 TypeScript & mocha & chai 写测试代码实战视频教程 #8 如何测试分页请求 - 如何使用 context(四更)
04:309Free使用 TypeScript & mocha & chai 写测试代码实战视频教程 #9 开启桌面 growl 通知功能 - 可选(第五更)
15:1110Pro使用 TypeScript & mocha & chai 写测试代码实战 #10 mocha 的 hook - before - beforeEach(六更)
05:43Free使用 TypeScript & mocha & chai 写测试代码实战 #11 测试更新 Post 功能
07:4812Free使用 TypeScript & mocha & chai 写测试代码实战 #12 只能更新自己创建的 Post
04:2613Pro使用 TypeScript & mocha & chai 写测试代码实战视频教程 #13 测试删除 Post(三更)
07:5314Pro使用 TypeScript & mocha & chai 写测试代码实战 #14 测试喜欢 Post 功能
15:4715Pro使用 TypeScript & mocha & chai 写测试代码实战视频教程 #15 修复测试逻辑代码 - 建议
18:1116Pro使用 TypeScript & mocha & chai 写测试代码实战视频教程 #16 测试创建和删除评论功能
07:2017Pro使用 TypeScript & mocha & chai 写测试代码实战视频教程 #17 测试覆盖率 - 完结
▬▬▬▬▬▬ 联系我 👋 ▬▬▬▬▬▬
知乎:https://www.zhihu.com/people/rails365
掘金:https://juejin.cn/user/1574156379888263
b 站:https://space.bilibili.com/31152817
Github:https://github.com/hfpp2012
Youtube:https://www.youtube.com/channel/UCA-Jkgr40A9kl5vsIqg-BIg
Discord:https://discord.gg/5rdjnEnU7F
Twitter:https://twitter.com/qiuzhi99pro
Facebook:https://twitter.com/qiuzhi99pro
Instagram:https://www.facebook.com/pro.qiuzhi/
▬▬▬▬▬▬ 微信相关 👋 ▬▬▬▬▬▬




© 汕尾市求知科技有限公司 | 专业版网站 | 在线学员:1129
粤公网安备 44152102000088号
| 粤ICP备19038915号