将猫鼬对象与对象进行比较时,Mocha测试会挂起

时间:2017-02-06 15:07:36

标签: node.js mongoose mocha chai-as-promised mockgoose

我正在使用mochachai(使用chai-as-promised),我正在尝试使用mockgoose来测试mongoDB数据库。

模特:

var Campaign = new Schema({
    //Schema here 
});

Campaign.statics.getOneById = function(id) {
    return this.findOne({_id: id }).populate('channels').populate('units');
};

正在测试的代码:

function getCampaignById(id) {
    return Campaign.getOneById(id);
}

测试代码:

var chai = require('chai'),
    chaiAsPromised = require('chai-as-promised'),
    mongoose = require('mongoose'),
    mockgoose = require('mockgoose'),
    Campaign = require('../src/campaigns/models/campaign-model'),
    Channel = require('../src/channels/models/channel-model'),
    Unit = require('../src/units/models/unit-model'),
    campaignRepo = require('../src/campaigns/lib/campaign-repository'),
    config = require('../scripts/services/config');

chai.use(chaiAsPromised);
chai.should();

before(function(done) {
    mockgoose(mongoose).then(function() {
        mongoose.connect(config.db.dbStr, function(err) {
            done(err);
        })
    })
});

describe('Campaign repository', function() {

    var fullCampaign = {
        country: {
            dst:1,
            timezone:'+02:00',
            code:'AX',
            name:'Åland Islands'},
        name: 'This is campaign name',
        startDate:'2017-02-19T22:00:00.000Z',
        endDate:'2017-02-27T22:00:00.000Z',
        creatives: ['creative'],
        units:[],
        channels:[],
        money: {
            action: {
                event: 'interaction_loaded',
                label: 'View'
            },
            currency: {
                code: 'USD',
                sign: '$'
            },
            budget: 11111,
            costPerAction: 2
        }
    };

    var newCampaign;

    it('should create a new campaign', function() {
         campaignRepo.create(fullCampaign).then(function(createdCampaign) {
            newCampaign = createdCampaign;
        });
    });

    it('should get the new campaign from the database', function() {
        return (campaignRepo.getCampaignById(newCampaign._id)).should.eventually.equal(newCampaign);
    });
});

问题是最后一次等式检查会挂起mocha:

  Campaign repository
    ✓ should create a new campaign
    1) should get the new campaign from the database


  1 passing (141ms)
  1 failing

当对非对象进行相同的测试时,

return (campaignRepo.getCampaignById(scopeNewCampaign._id)).should.eventually.equal('just a string');

mocha正常失败。

  Campaign repository
    ✓ should create a new campaign
    1) should get the new campaign from the database


  1 passing (141ms)
  1 failing

  1) Campaign repository should get the new campaign from the database:
     AssertionError: expected { Object ($__, isNew, ...) } to equal 'just a string'

1 个答案:

答案 0 :(得分:0)

不确定这是否是问题的根源,但这是为了评论。

首先,您不会在第一个测试用例中返回承诺。因此,mocha不会等到你的对象被创建。这可能是您第二次测试失败的原因。所以试试这个:

it('should create a new campaign', function() {
     return campaignRepo.create(fullCampaign).then(function(createdCampaign) {
        newCampaign = createdCampaign;
    });
});

其次,让你的测试相互依赖是非常糟糕的风格。如果每个测试用例都可以独立运行,那就更清晰了。否则,如果第一个错误并且您不明白为什么,则所有连续测试都会失败。这在第一次设置时可能看起来有点多,但是它的def。从长远来看值得。否则你会发现自己对实际上正常的失败测试感到困惑。