【发布时间】:2022-01-11 17:00:13
【问题描述】:
我不明白为什么这些都失败了我尝试过的变体是:
response.body.should.be.a('object').and.to.have.property('id');
和
response.body.should.have.property('id');
我得到了错误 未捕获的 AssertionError:预期 { Object (starss) } 具有属性 'id'
这是我来自 Postman 的 JSON 负载
{
"starss": {
"id": 1,
"name": "1",
"discovery_name": "1",
"imageFileName": "test1.jpg",
"datediscovered": "2001-01-01T00:00:00.000Z",
"colour": "1",
"mass": "1.000",
"dfe": "1.000",
"galaxy_origin": "1",
"star_categories_id": 1,
"createdAt": "2021-12-03",
"updatedAt": "2021-12-12"
}
}
这是我的摩卡测试
describe('GET/stars/:id',() => {
it("it should get one star by ID", (done) =>
{
// Uncaught AssertionError: expected { Object (starss) } to be a starss
// star GET by ID
const starId = 1;
chai.request(server)
.get("/stars/" + starId )
.end((err,response)=>{
response.should.have.status(200);
response.body.should.be.a('object');
response.body.should.be.a('object').and.to.have.property('id');
response.body.should.have.property('id');
// response.body.should.have.property('name');
// response.body.should.have.property('discovery_name');
// response.body.should.have.property('imageFileName');
// response.body.should.have.property('datediscovered');
// response.body.should.have.property('colour');
// response.body.should.have.property('mass');
// response.body.should.have.property('dfe');
// response.body.should.have.property('galaxy_origin');
// response.body.should.have.property('star_categories_id');
done();
});
});
【问题讨论】:
标签: javascript unit-testing mocha.js chai chai-http