将Faker.js用于数组时出现AssertionError

时间:2019-02-15 04:45:57

标签: node.js mocha chai faker

我在使用 mocha chai 测试我的 node.js 应用时,正在使用faker.js创建虚假数据,并且正在获得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,则测试通过,但类别必须是数组。 我在代码中写错了吗?

1 个答案:

答案 0 :(得分:0)

我想categories检查失败。在这种情况下,请始终使用deep.equal(别名为eql)比较对象和数组。因此,应该是

expect(resVenue.categories).to.eql(venue.categories);

// or 

expect(resVenue.categories).to.deep.equal(venue.categories);

参考: