2017-05-14 7 views
1

私は、約束が解決した後に実行するいくつかのコードを持っています。私は、約束した解決策を呼び出すメソッドを偵察しようとしていますが、スパイオンメソッドの呼び出しが呼び出される前に、テストの実行が終了して問題が発生しています。約束の後にSinonを使用して解決する

実際にstorage.addへの呼び出しをテストできますか?

_execute: function() { 
    this.updateServiceApi().then(function(){ 
     // tests finish before this code is executed :(
     storage.add("name", true); 
    }); 
}, 
+0

どのテストランナーを使用していますか? –

答えて

0

ここでの問題は_executeが同期であるかのように、あなたのテストを書いているということです。

は、ここでのテストです:

"it marks true in storage": function() { 
    sinonSandbox.stub(tested, "updateServiceApi", function() { 
     return MPromise.resolve(); 
    }); 

    var storageStub = sinonSandbox.stub(storage, "add"); 

    tested._execute(); 

    expect(
     storageStub.calledWith("name", true) 
    ).to.be.true 
} 

そして、ここでの実装です。

storage.add()コールが行われる前に約束のため、expectコールが実行されます。

これはテストランナーに依存しますが、一般的な修正では、expectコードを含むテスト内の呼び出された関数に.then()を追加することです。

"it marks true in storage": function() { 
    sinonSandbox.stub(tested, "updateServiceApi", function() { 
     return MPromise.resolve(); 
    }); 

    var storageStub = sinonSandbox.stub(storage, "add"); 

    return tested._execute() 
     .then(() => { 
      expect(storageStub.calledWith("name", true)).to.be.true; 
     }); 
} 
関連する問題