2017-02-06 4 views
0

私はmochachaichai-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); 
    }); 
}); 

問題は、最後の等式チェックがモカハングということです。

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' 

答えて

0

あなたの問題の根本原因はわかりませんが、これはコメントのためのものです。

最初に、最初のテストケースで約束を返さないでください。したがって、モカはあなたのオブジェクトが作成されるまで待つことはありません。これは、あなたの2回目のテストの失敗の原因かもしれません。したがって、これを試してください:

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

第2に、あなたのテストをお互いに依存させることは非常に悪いスタイルです。すべてのテストケースが単独で実行できる場合は、はるかにクリーンです。それ以外の場合は、最初の1つのエラーと理由が表示されない場合、すべての連続したテストが失敗します。これは、最初のセットアップでは多少の作業に見えるかもしれませんが、defです。長期的には価値がある。そうでなければ、実際にうまくいかないテストの失敗を自分自身が混乱してしまうでしょう。

+0

残念なことにあなたの提案はうまくいかず、私が述べたように、問題はテストが失敗した以上のものです。テストスイート全体が端末をハングするだけです。 あなたは絶対に正しいですが、私はこれらのテストを書き始めたばかりで、この問題が発生したので、テストを完全には設計しませんでした。 – dabn

+0

データベース接続はまったくありますか? –

+0

別のものは私が気づいたように、オブジェクトを比較するときには深い等価性を使用する必要があります: '.should.eventually.deep.equal' –

関連する問題