我在用sinon和chai編寫javascript測試時遇到問題。 我想檢查是否一個函數被調用的間諜,並得到「錯誤:無效的柴屬性:calledOnce」當調用calledOnce時無效Chai屬性
我做在同一個測試依賴另一個項目同樣的事情,沒有任何問題...
var udpSocketStub = this.sandbox.spy(udpSocket, 'send');
expect(udpSocketStub).calledOnce; // SHOULD FAIL
"dependencies": {
"body-parser": "~1.17.1",
"bootstrap": "^4.0.0-alpha.6",
"chai": "^4.1.0",
"co-mocha": "^1.2.0",
"cookie-parser": "~1.4.3",
"debug": "~2.6.3",
"express": "~4.15.2",
"jquery": "^3.2.1",
"mocha": "^3.4.2",
"morgan": "~1.8.1",
"node-compass": "0.2.3",
"pug": "^2.0.0-rc.1",
"serve-favicon": "~2.4.2",
"sinon": "^2.3.8",
"sinon-chai": "^2.12.0"
}
你確定嗎?我認爲它應該是'expect(udpSocketStub.send.calledOnce).to.be.true',如果我錯了,請糾正我。 – hinok
@hinok http://ricostacruz.com/cheatsheets/sinon-chai.html看起來你不需要'to.be.true'。 –
@Elliott sinon-chai,我錯過了這個套餐,你說得對。但仍然可能它應該是'expect(udpSocketStub.send).calledOnce;' – hinok