1)第一个测试套件案例运行良好,完全没有错误。
chai.request(app) .get('/api/courses') .end((err, res) => {
res.should.have.status(200);
res.body.should.be.a("json");
done(); });
2)尽管,尽管没有任何变化,但仍说“ AssertionError:预期[Array(3)]是json”。
describe("Courses", () => {
describe("GET /api/courses", () => {
it("should get all the courses", (done) => {
chai.request(app)
.get('/api/courses')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a("json");
done();
});
})
});
});
我只是插入了describe及其功能,以提供有关测试的更多信息。
答案 0 :(得分:0)
无论如何,您想通过该测试实现什么?
编辑:对不起,我不想将此作为答案发布。但是虽然我已经在这里:
据我所知,chai可以按预期工作,因为您收到的返回值实际上是一个数组,而不是一个json(尽管NodeJs中的json文件可以是一个数组)。这就是测试失败的原因。如果要检查响应的实际标题,则可以测试res.headers。 为什么不能在mocha测试之外运行,我无法告诉您,但是感觉像是一个显示-而不是实际的错误,就像您如何实际测试断言是否已正确验证一样?
答案 1 :(得分:0)
我认为我们无法使用该命令检查body是否为json。
res.body.should.be.a("json"); // invalid
如果您查看chai documentation或here,则无法在a()
函数中指定 json 。
另一种解决方案是,您可以检查res.headers
中的json,还可以检查响应主体是否为数组,例如
chai
.request(app)
.get("/api/courses")
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
res.headers["content-type"].should.contains('application/json');
done();
});
希望有帮助