[英]Test always passing with Sinon and Chai
我正在使用 Mocha、Chai 和 Sinon 來測試一些 Node 方法。
這個測試通過了,當我將 '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();
})
});
請注意,我在第二個測試中測試了“callTwice”。 它仍然通過。 如果我將其更改為“notCalled”,它仍然會通過。 基本上它總是通過。
我錯過了什么?
我可以重現您報告的行為的唯一方法是,如果我忘記調用chai.use
來添加 Sinon 的斷言。 例如,這按預期工作(測試失敗):
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;
});
但是如果你用同樣的代碼注釋掉chai.use(sinonChai)
,那么測試就會通過!
為了好玩,你可以嘗試expect(stub).to.have.been.platypus
,它也會通過。 Chai 的expect
接口容忍無意義的標識符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.