post 路由测试失败
post route test failing
describe("POST /post methods", () => {
it("should get /post ", (done) => {
const testing = {
name: "charanjit",
content: "im posting",
giph: ""
};
chai.request(server)
.post("/posts")
.send(testing)
.expect({
id: 4,
...testing
}, done)
Server.js
server.post("/posts", (req, res) => {
const incomingRequest = req.body;
if (isValidPost(incomingRequest)) {
const post = {
name: incomingRequest.name.toString(),
content: incomingRequest.content.toString(),
giph: incomingRequest.gif.toString(),
date: new Date(),
likes: 0,
dislikes: 0,
laughs: 0,
comments: [],
//id : database.length
};
当我 运行 测试时我得到 TypeError: chai.request(...).post(...).send(...).expect is not a function
.
我尝试按照在线教程进行操作,但在测试 post 请求时总是出现错误,有人可以告诉我哪里错了吗?
您必须在调用中包含 expect
。
根据 docs 调用应该是:
chai.request(app)
.put('/user/me')
.send({ password: '123', confirmPassword: '123' })
.end(function (err, res) {
expect(err).to.be.null;
expect(res).to.have.status(200);
});
所以尝试这样的事情:
chai.request(server)
.post("/posts")
.send(testing)
.end(function (err, res) {
expect(...)
done()
});
describe("POST /post methods", () => {
it("should get /post ", (done) => {
const testing = {
name: "charanjit",
content: "im posting",
giph: ""
};
chai.request(server)
.post("/posts")
.send(testing)
.expect({
id: 4,
...testing
}, done)
Server.js
server.post("/posts", (req, res) => {
const incomingRequest = req.body;
if (isValidPost(incomingRequest)) {
const post = {
name: incomingRequest.name.toString(),
content: incomingRequest.content.toString(),
giph: incomingRequest.gif.toString(),
date: new Date(),
likes: 0,
dislikes: 0,
laughs: 0,
comments: [],
//id : database.length
};
当我 运行 测试时我得到 TypeError: chai.request(...).post(...).send(...).expect is not a function
.
我尝试按照在线教程进行操作,但在测试 post 请求时总是出现错误,有人可以告诉我哪里错了吗?
您必须在调用中包含 expect
。
根据 docs 调用应该是:
chai.request(app)
.put('/user/me')
.send({ password: '123', confirmPassword: '123' })
.end(function (err, res) {
expect(err).to.be.null;
expect(res).to.have.status(200);
});
所以尝试这样的事情:
chai.request(server)
.post("/posts")
.send(testing)
.end(function (err, res) {
expect(...)
done()
});