2016-08-22 6 views
1

私は比較的シンプルなmocha &チャイテストセットアップを持っています。残念なことに、私がmochaを走らせると、間違いなく失敗するテストがパスします!ここに私のテストケースがあります。トライキャッチでexpect(true).to.equal(false)ラッピングdescribe()でテストを折り返すとモカが失敗しない

var expect = require('chai').expect; 
var nock = require('nock'); 
var request = require('request'); 

var testUrl = 'http://test.wicked.ti'; 
var getItems = function(url, callback) {    
    request.get(url + '/items',function(err, data) { 
    if(err) throw err; 
    callback(data); 
    }); 
}; 

describe('Sample Unit Tests', function(){ 
    it('I am making sure the correct rest endpoint is called', function() { 
    var request = nock(testUrl) 
     .get('/items') 
     .reply(200, {}); 

    getItems(testUrl, function(data) { 
     console.log(data);   // This runs 
     expect(true).to.equal(false); // This should always fail! 
     done(); 
    }); 
    }); 
}); 

はうまくキャッチされている(下図参照)エラーがスローされます。それは

it('I am making sure the correct rest endpoint is called', function() { 
    var request = nock(testUrl) 
     .get('/items') 
     .reply(200, {}); 

    getItems(testUrl, function(data) { 
     console.log(data);   // This runs 

     // Adding try/catch block 
     try { expect(true).to.equal(false); } catch(err) { console.error(err) } 

     done(); 
    }); 

され、これが記録されたエラー

{ [AssertionError: expected true to equal false] 
    message: 'expected true to equal false', 
    showDiff: true, 
    actual: true, 
    expected: false } 

私は成功せず、間違っているかもしれないものを把握しようとしている私の髪をラッキングされています!問題は私が逃していることです。それが助けであれば、私はこれを外側に書き込もうとしました。describe() & it()ブロックであり、ちょうどいいです。

+1

ことは、私はしませんでした「何か」、機能(行って)私が行方不明になったマイナーなディテールた{... ' –

答えて

3

これは、同期テストを実行しているために、非同期機能が完了するのを待たないためです。それはあなたのコールバックは引数を必要と非同期にするには:?!

it('...', function(done) { 
    //     | 
    //     | 
    //  this is where "done" comes from and it's 
    //   the missing bug in your code 
+0

おかげで('それを試してみてください – Willa

+1

@Willa:https://mochajs.org/#asynchronous-codeこれは簡単に見逃せません:実際のところ、ほとんどの人は、すべての関数に '関数が定義する引数の数を反映した.length'プロパティを返します。コールバックに渡す値の数を調べるためにライブラリに使用しました。非同期。 – slebetman

関連する問題