Node.js 西农和柴总是通过考试
我正在使用Mocha、Chai和Sinon测试一些节点方法 此测试通过,当我将“calledOnce”更改为“calledTwice”时,它会按预期失败Node.js 西农和柴总是通过考试,node.js,mocha.js,sinon,chai,Node.js,Mocha.js,Sinon,Chai,我正在使用Mocha、Chai和Sinon测试一些节点方法 此测试通过,当我将“calledOnce”更改为“calledTwice”时,它会按预期失败 it('should call checkIfRoomExists once', function (done) { var check = sandbox.spy(RoomInfoModel, 'checkIfRoomExists'); ViewBusiness.getViewToRender
it('should call checkIfRoomExists once', function (done) {
var check = sandbox.spy(RoomInfoModel, 'checkIfRoomExists');
ViewBusiness.getViewToRender("thisisanoneknownroom", function (viewName) {
expect(check.calledOnce).to.equal(true);
done();
})
});
但是,当我尝试学习教程时,“expect”的设置如下:
it('should call checkIfRoomExists once', function (done) {
var check = sandbox.spy(RoomInfoModel, 'checkIfRoomExists');
ViewBusiness.getViewToRender("thisisanoneknownroom", function (viewName) {
expect(check).to.have.been.calledTwice;
done();
})
});
请注意,我正在第二个测试中测试“calledTwice”。它还是过去了。如果我把它改成‘notCalled’,它仍然会通过。基本上它总是过去
我错过了什么 我能重现您报告的行为的唯一方法是,如果我忘记调用
chai。请使用将Sinon的断言添加到其中。例如,这可以按预期工作(测试失败):
但是,如果您使用相同的代码并注释掉chai.use(sinonChai)
,那么测试将通过
为了好玩,你可以尝试expect(stub).to.have.been.platypus
,这也会过去的。Chai的expect
接口容忍无意义的标识符。我应该补充一点,这与测试改变状态无关。我一直在用一个替换另一个,并拆掉沙箱。谢谢,这就解决了它!我不确定我对考试通过的看法,因为expect语句中有一个拼写错误。是的,这是个问题。我经常使用Chai,但从不使用should
界面,也很少使用expect
界面。当我贡献给已经使用它的第三方代码时,我使用它。对于我自己的代码,我使用assert
接口。它不尝试对属性进行任何处理,因此如果使用错误的方法名,它将很难失败。
const sinon = require("sinon");
const chai = require("chai");
const sinonChai = require("sinon-chai");
chai.use(sinonChai); // This is crucial to get Sinon's assertions.
const expect = chai.expect;
it("test", () => {
const stub = sinon.stub();
stub();
expect(stub).to.have.been.calledTwice;
});