Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调用calledOnce时Chai属性无效_Javascript_Sinon_Sinon Chai - Fatal编程技术网

Javascript 调用calledOnce时Chai属性无效

Javascript 调用calledOnce时Chai属性无效,javascript,sinon,sinon-chai,Javascript,Sinon,Sinon Chai,我与sinon和chai在用javascript编写测试时遇到问题。 我试图检查是否对间谍调用了函数,并得到“Error:Invalid Chai property:calledOnce” 我在另一个项目中使用相同的测试依赖项做同样的事情,没有任何问题 var udpSocketStub = this.sandbox.spy(udpSocket, 'send'); expect(udpSocketStub).calledOnce; // SHOULD FAIL "dependencies":

我与sinon和chai在用javascript编写测试时遇到问题。 我试图检查是否对间谍调用了函数,并得到“Error:Invalid Chai property:calledOnce”

我在另一个项目中使用相同的测试依赖项做同样的事情,没有任何问题

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"
}

您只是缺少了向chai添加类似sinon断言的
sinon-chai

npm install --save sinon-chai
初始化:

var chai = require('chai');
var sinon = require('sinon');
chai.use(require('sinon-chai'));
如果您想知道,使用存根或原始函数都可以:

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;

你确定吗?我认为应该是
expect(udpSocketStub.send.calledOnce).to.be.true
,如果我错了,请纠正我。@hinok看起来你不需要
to.be.true
@Elliott sinon chai,我错过了那个包,你是对的。但仍然可能是
expect(udpSocketStub.send)谢谢你的回答,因为这解决了我的问题…你介意解释一下或告诉我如何做
chai.use(require('sinon-chai'))行如果您试图执行
导入
而不是
要求
?您可以尝试
从“sinon chai”导入sinonChai
然后
chai。使用(sinonChai)
但您使用的是TypeScript,对吗?我不确定柴信农是否支持打字。如果您觉得我解决了您的问题,请随意勾选绿色箭头,将问题标记为已回答;)你确实解决了我的问题,但不幸的是我不是OP