繁体   English   中英

测试总是通过诗农和柴

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM