对数组使用 Faker.js 时出现 AssertionError
AssertionError when using Faker.js for arrays
我在使用 mocha 测试我的 node.js 应用程序时使用 faker.js 创建虚假数据,并且chai,我得到一个 AssertionError
。
AssertionError: expected [ 'mint green' ] to equal ["mint green"]
我无法弄清楚是什么造成了差异,也无法在网上的任何地方找到提示。也没有遇到任何有同样问题的帖子。
这是我的场地模型:
'use strict'
const mongoose = require('mongoose');
const venueSchema = mongoose.Schema({
name: {
type: String,
required: true
},
categories: [String],
contact: {
phone: String,
address: String,
coordinates: {
lat: Number,
lng: Number
}
},
created: {
type: Date,
default: Date.now
}
})
venueSchema.methods.serialize = function() {
return {
id: this._id,
name: this.name,
categories: this.categories,
contact: this.contact,
created: this.created
}
}
const Venue = mongoose.model('Venue', venueSchema);
module.exports = { Venue };
这里是我使用的地方 faker:
function generateVenueData(){
return {
name: faker.lorem.words(),
categories: [faker.commerce.color()],
contact: {
phone: faker.phone.phoneNumber(),
address: faker.address.streetAddress(),
coordinates: {
lat: faker.address.latitude(),
lng: faker.address.longitude()
}
},
created: faker.date.past()
}
}
这是我测试失败的部分:
it('should return venues with the right fields', function(){
let resVenue;
return chai.request(app)
.get('/api/venues')
.then(function(res){
// console.info(res)
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res.body.venues).to.be.a('array');
expect(res.body.venues).to.have.lengthOf.at.least(1);
res.body.venues.forEach(function(venue){
console.info(venue)
expect(venue).to.be.a('object');
expect(venue).to.include.keys('id', 'name', 'categories', 'contact', 'created');
})
resVenue = res.body.venues[0];
return Venue.findById(resVenue.id);
})
.then(function(venue){
// console.info(resVenue);
// console.info(venue);
expect(resVenue.id).to.equal(venue.id);
expect(resVenue.name).to.equal(venue.name);
expect(resVenue.categories).to.equal(venue.categories);
console.info(typeof resVenue.categories);
console.info(typeof venue.categories);
expect(resVenue.contact.phone).to.equal(venue.contact.phone);
expect(resVenue.contact.address).to.equal(venue.contact.address);
expect(resVenue.contact.coordinates.lat).to.equal(venue.contact.coordinates.lng);
expect(resVenue.created).to.not.be.null;
})
})
如果我删除模型中 String
周围的 []
,则测试会通过,但类别需要是一个数组。
我的代码写错了吗?
我想 categories
检查失败了。在那种情况下,始终使用 deep.equal
(别名 eql
)来比较对象和数组。所以,应该是
expect(resVenue.categories).to.eql(venue.categories);
// or
expect(resVenue.categories).to.deep.equal(venue.categories);
参考:
我在使用 mocha 测试我的 node.js 应用程序时使用 faker.js 创建虚假数据,并且chai,我得到一个 AssertionError
。
AssertionError: expected [ 'mint green' ] to equal ["mint green"]
我无法弄清楚是什么造成了差异,也无法在网上的任何地方找到提示。也没有遇到任何有同样问题的帖子。
这是我的场地模型:
'use strict'
const mongoose = require('mongoose');
const venueSchema = mongoose.Schema({
name: {
type: String,
required: true
},
categories: [String],
contact: {
phone: String,
address: String,
coordinates: {
lat: Number,
lng: Number
}
},
created: {
type: Date,
default: Date.now
}
})
venueSchema.methods.serialize = function() {
return {
id: this._id,
name: this.name,
categories: this.categories,
contact: this.contact,
created: this.created
}
}
const Venue = mongoose.model('Venue', venueSchema);
module.exports = { Venue };
这里是我使用的地方 faker:
function generateVenueData(){
return {
name: faker.lorem.words(),
categories: [faker.commerce.color()],
contact: {
phone: faker.phone.phoneNumber(),
address: faker.address.streetAddress(),
coordinates: {
lat: faker.address.latitude(),
lng: faker.address.longitude()
}
},
created: faker.date.past()
}
}
这是我测试失败的部分:
it('should return venues with the right fields', function(){
let resVenue;
return chai.request(app)
.get('/api/venues')
.then(function(res){
// console.info(res)
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res.body.venues).to.be.a('array');
expect(res.body.venues).to.have.lengthOf.at.least(1);
res.body.venues.forEach(function(venue){
console.info(venue)
expect(venue).to.be.a('object');
expect(venue).to.include.keys('id', 'name', 'categories', 'contact', 'created');
})
resVenue = res.body.venues[0];
return Venue.findById(resVenue.id);
})
.then(function(venue){
// console.info(resVenue);
// console.info(venue);
expect(resVenue.id).to.equal(venue.id);
expect(resVenue.name).to.equal(venue.name);
expect(resVenue.categories).to.equal(venue.categories);
console.info(typeof resVenue.categories);
console.info(typeof venue.categories);
expect(resVenue.contact.phone).to.equal(venue.contact.phone);
expect(resVenue.contact.address).to.equal(venue.contact.address);
expect(resVenue.contact.coordinates.lat).to.equal(venue.contact.coordinates.lng);
expect(resVenue.created).to.not.be.null;
})
})
如果我删除模型中 String
周围的 []
,则测试会通过,但类别需要是一个数组。
我的代码写错了吗?
我想 categories
检查失败了。在那种情况下,始终使用 deep.equal
(别名 eql
)来比较对象和数组。所以,应该是
expect(resVenue.categories).to.eql(venue.categories);
// or
expect(resVenue.categories).to.deep.equal(venue.categories);
参考: