最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

将Faker.js用于数组时的AssertionError

运维笔记admin18浏览0评论

将Faker.js用于数组时的AssertionError

将Faker.js用于数组时的AssertionError

我正在使用faker.js在使用mocha和chai测试我的node.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: [fakermerce.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);

参考:

  • https://www.chaijs/api/bdd/#eqlobj-msg
发布评论

评论列表(0)

  1. 暂无评论