![](/img/trans.png)
[英]Angular $q promise Unit testing using Mocha, Chai, Sinon
[英]Unit testing MOCHA SINON CHAI (check call nested functions)
如果有人了解測試,請告訴我如何實現兩件事的測試:
1)在makeRing函數啟動時調用了obj.newRing方法。 2)參數'num'是否傳遞給函數makeRing( num )是否與在obj.newRing({ number:num })中傳遞的對象的屬性匹配。
function makeRing (num) {
currRing = obj.newRing ({number: num});
}
也許有人會對如何使用sinon有一些想法,否則在這種情況下,我將很高興收到任何信息。 我受了很長時間...謝謝!
如果您可以在測試中訪問obj
,則可以執行以下操作:
// create a spy for your function:
const newRingSpy = sinon.spy();
// replace the real function with the spy:
sinon.stub(obj, 'newRing', newRingSpy);
// run the test:
makeRing(7);
// 1) validate that obj.newRing was called exactly once:
expect(newRingSpy.calledOnce).to.be(true);
// 2) and/or validate the arguments it was called with:
expect(newRingSpy.firstCall.args).to.eql([{number: 7}]);
如果您只想知道是否完全調用了該函數,則第二項檢查已將其覆蓋(如果未調用該函數,則newRingSpy.firstCall
為null)。
如果您無權訪問obj
,那么將生產代碼更改為以下內容可能是最好的策略:
function makeRing (num, obj) {
currRing = obj.newRing ({number: num});
}
然后,您可以在測試中輕松將存根的obj
傳遞給makeRing()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.