2017-10-10 17 views
0

他の質問を見ると、実際に問題の原因を見つけることはできません。私はモカを使ってテストしようとしています。このMochaテストでdone()コールバックが呼び出されていることを確認してください

it("Should not do the work",function(done) { 
    axios 
    .post("x/y",{ id:a2 }) 
    .then(function(res) { 
     assert(false,"Should not do the work"); 
     done(); 
    }) 
    .catch(function(res) { 
     assert.equal(HttpStatus.CONFLICT,res.status); 
     done(); 
    }); 
}); 

it("Should do the work",function(done) { 
    axios 
    .post("/x/y",{ id: a1 }) 
    .then(function(res) { 
     done(); 
    }) 
    .catch(done); 
}); 

結果は以下のとおりであった:

√ Should not do the work (64ms) 
1) Should do the work 
1 passing (20s) 
1 failing 

1) Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. 

タイムアウトは動作しませんでし増やします。

+0

あなたは単にモカの約束を「返す」ことができますので、それに応じて対処します。最初の例では、それらのブロックが実際に実行されていることを確認していますか?私はそれがまったくトリガしていることを確認したいと思います。 – tadman

答えて

0

Mochaで約束しているreturnを忘れずにそれを処理することを忘れないでください。最初の例では、それらのブロックが実際に実行されていることを確認していますか?

アサーションを実行すると例外が発生する可能性があります。これにより、実行しようとしていることを妨害する可能性があります。あなたの約束図書館がそれをサポートしていれば、いつでもそうすることができます:

it("Should not do the work",function(done) { 
axios.post("x/y",{ id:a2 }) 
    .then(function(res) { 
    assert(false,"Should not do the work"); 
    }) 
    .catch(function(res) { 
    assert.equal(HttpStatus.CONFLICT,res.status); 
    }) 
    .finally(done); 
}); 

これは必ず行う必要があります。

さらに良い:漁獲量におけるアサーションとアサーションの漁獲を行うための

it("Should not do the work",function() { 
    return axios.post("x/y",{ id:a2 }) 
    .then(function(res) { 
     assert(false,"Should not do the work"); 
    }) 
    .catch(function(res) { 
     assert.equal(HttpStatus.CONFLICT,res.status); 
    }) 
}); 

ウォッチ。より良い計画は非同期であるかもしれません:

it("Should not do the work", async function() { 
    var res = await axios.post("x/y",{ id:a2 }) 

    assert.equal(HttpStatus.CONFLICT,res.status); 
}); 
+0

最初の例は正常に動作しています。私は第2の例で問題に直面しています –

+0

これはうまくいくはずなので実装が破棄されているかもしれません。 – tadman

関連する問題