1

我正在使用mochaand 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'
4

1 回答 1

0

不确定这是否是您问题的根源,但这很需要评论。

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

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

其次,imo 让您的测试相互依赖是非常糟糕的风格。如果每个测试用例都可以独立运行,它会更干净。否则,如果第一个错误并且您不明白原因,则所有连续测试都会失败。这在第一次设置时可能看起来有点工作,但它是 def. 从长远来看值得。否则,您会发现自己对失败的测试感到困惑,实际上这些测试都很好。

于 2017-02-06T15:42:50.613 回答