首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mocha测试在比较mongoose对象和对象时挂起

Mocha测试在比较mongoose对象和对象时挂起
EN

Stack Overflow用户
提问于 2017-02-06 23:07:36
回答 1查看 450关注 0票数 1

我正在使用mochachai (带有chai-as-promised),并且我正在尝试使用mockgoose测试一个mongoDB数据库。

模型:

代码语言:javascript
复制
var Campaign = new Schema({
    //Schema here 
});

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

测试中的代码:

代码语言:javascript
复制
function getCampaignById(id) {
    return Campaign.getOneById(id);
}

测试代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
  Campaign repository
    ✓ should create a new campaign
    1) should get the new campaign from the database


  1 passing (141ms)
  1 failing

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

代码语言:javascript
复制
return (campaignRepo.getCampaignById(scopeNewCampaign._id)).should.eventually.equal('just a string');

mocha只是正常地失败了。

代码语言:javascript
复制
  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'
EN

回答 1

Stack Overflow用户

发布于 2017-02-06 23:42:51

不确定这是否是你的问题的根源,但这是一个渴望的评论。

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

代码语言:javascript
复制
it('should create a new campaign', function() {
     return campaignRepo.create(fullCampaign).then(function(createdCampaign) {
        newCampaign = createdCampaign;
    });
});

其次,我认为让你的测试相互依赖是非常糟糕的风格。如果每个测试用例都能独立运行,那就干净多了。否则,如果第一个测试出错,那么所有连续的测试都会失败,而您看不到原因。在第一次设置时,这可能看起来有点繁重,但这是默认的。从长远来看是有价值的。否则,你会发现自己对失败的测试感到困惑,而这些测试实际上是好的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42070983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档