testing总是与诗乃和柴一起传递

我正在使用Mocha,Chai和Sinon来testing一些Node方法。

这个testing通过,当我把'calledOnce'改为'calledTwice'时,它会按预期的那样失败。

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(); }) }); 

但是,当我尝试并按照教程“设定”是这样设置的:

 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(); }) }); 

请注意,我在第二个testing中正在testing“calledTwice”。 它仍然通过。 如果我将它更改为“notCalled”,它仍然通过。 基本上它总是通过。

我错过了什么?

我可以重现你所报告的行为的唯一方法是,如果我忘记调用chai.use来添加Sinon的断言。 例如,这可以按预期工作(testing失败):

 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; }); 

但是如果你把相同的代码和注释掉了,那么testing就会通过!


为了好玩,你可以尝试expect(stub).to.have.been.platypus ,这也将通过。 柴的expect接口容忍无意义的标识符。