gpt4 book ai didi

node.js - 断言错误: expected { Object (__flags) } to have property '_id'

转载 作者:太空宇宙 更新时间:2023-11-03 21:54:12 25 4
gpt4 key购买 nike

我正在使用 mocha 和 chai 测试我的代码。我的代码如下所示:

let mongoose = require("mongoose");
let Item = require('./../model/items.js');

let chai = require('chai');
let chaiHttp = require('chai-http');
let server = require('../app.js');
let should = chai.should();

chai.use(chaiHttp);

describe('Items', () => {
beforeEach((done) => {
Item.remove({}, (err) => {
done();
});
});
describe('/GET item', () => {
it('it should GET perticular item', (done) => {
chai.request("http://localhost:3000")
.get('/foodtrucks/items?foodtruck_id=594f908042357813bc9d198d')
.end((err, res) => {
res.body.should.have.property('status').eql('200');
res.body.should.have.property('message');
res.body.should.have.property('data').should.
be.a('object').should.have.property('_id');
done();
});
});
});

});

我得到的回复是:

{
"status": "200",
"message": "Item List",
"data": {
"_id": "594f908042357813bc9d198d",
"foodtruck_logo": "",
"foodtruck_img": "",
"foodtruck_tag": "best restaurent in the world",
"foodtruck_name": "world in a box",
"item_list": [
{
"_id": "594f9b4e8df2042df02d58aa",
"item_img": "https://myWebsite.com/item_img-1499429774105.jpeg",
"item_price": 5.99,
"item_discount_price": 4.55,
"item_category": "Chicken",
"item_description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus volutpat urna nec placerat rhoncus.",
"item_tag": "",
"item_name": "chilli chicken",
"no_of_likes": 1,
"item_quantity_ordered": 1,
"item_stock": 2,
"item_illustrations": [
"spicy",
"non-veg"
]
},
.....
]
}
}

res.body.should.have.property('data').should.be.a('object').should.have.property('_id') 给出我的错误。是不是写错了什么?

最佳答案

您不能嵌套should,因为它修改Object.prototype的方式。

相反,使用这样的东西:

res.body.should.have.property('data')
.which.is.an('object')
.and.has.property('_id')

关于node.js - 断言错误: expected { Object (__flags) } to have property '_id' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45231826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com