admin管理员组文章数量:1180551
I'm having problems writing tests in JavaScript with Sinon and Chai. I'm trying to check if a function is called on a spy and get I get
"Error: Invalid Chai property: calledOnce"
I'm doing the same thing in another project with the same test dependencies without any problem.
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"
}
I'm having problems writing tests in JavaScript with Sinon and Chai. I'm trying to check if a function is called on a spy and get I get
"Error: Invalid Chai property: calledOnce"
I'm doing the same thing in another project with the same test dependencies without any problem.
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"
}
Share
Improve this question
edited Oct 22, 2021 at 16:33
VLAZ
29k9 gold badges62 silver badges82 bronze badges
asked Jul 25, 2017 at 22:07
user1716970user1716970
7651 gold badge8 silver badges19 bronze badges
3
|
1 Answer
Reset to default 36You're just missing the sinon-chai
package, that adds sinon-like assertions to chai.
npm install --save sinon-chai
Initialization:
var chai = require('chai');
var sinon = require('sinon');
chai.use(require('sinon-chai'));
In case you're wondering, using the stub or the original function both work:
var expect = chai.expect;
var udpSocketStub = this.sandbox.spy(udpSocket, 'send');
// Make a call
updSocket.send({..});
// Both should pass
expect(udpSocketStub).calledOnce;
expect(udpSocket.send).calledOnce;
// Identical, but more readable
expect(udpSocketStub).to.have.been.calledOnce;
expect(udpSocket.send).to.have.been.calledOnce;
本文标签: javascriptInvalid Chai property when calling calledOnceStack Overflow
版权声明:本文标题:javascript - Invalid Chai property when calling calledOnce - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738117632a2064762.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
expect(udpSocketStub.send.calledOnce).to.be.true
, correct me if I'm wrong. – Dawid Karabin Commented Jul 25, 2017 at 22:13to.be.true
. – Elliott Beach Commented Jul 26, 2017 at 1:41expect(udpSocketStub.send).calledOnce;
– Dawid Karabin Commented Jul 26, 2017 at 8:57