我正在使用mocha
and chai
(with chai-as-promised
) 并且我正在尝试使用mockgoose
.
该模型:
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);
});
});
问题是最后一个相等检查挂起摩卡:
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');
摩卡咖啡只是正常失败。
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'