简体   繁体   English

测试总是通过诗农和柴

[英]Test always passing with Sinon and Chai

I am using Mocha, Chai and Sinon to test some Node methods.我正在使用 Mocha、Chai 和 Sinon 来测试一些 Node 方法。

This test passes and when I change 'calledOnce' to 'calledTwice' it fails as expected.这个测试通过了,当我将 '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();
            })
        });

However when I try and follow tutorials the 'expect' is set up like this:但是,当我尝试按照教程进行操作时,“期望”设置如下:

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

Note that I am testing for 'calledTwice' in the second test.请注意,我在第二个测试中测试了“callTwice”。 It still passes.它仍然通过。 If I change it to 'notCalled' it still passes.如果我将其更改为“notCalled”,它仍然会通过。 Basically it always passes.基本上它总是通过。

What am I missing?我错过了什么?

The only way I can reproduce the behavior you report is if I forget to call chai.use to add Sinon's assertions to it.我可以重现您报告的行为的唯一方法是,如果我忘记调用chai.use来添加 Sinon 的断言。 For instance, this works as expected (the test fails):例如,这按预期工作(测试失败):

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

But if you take the same code and comment out chai.use(sinonChai) , then the test will pass!但是如果你用同样的代码注释掉chai.use(sinonChai) ,那么测试就会通过!


For fun, you can try expect(stub).to.have.been.platypus and that will pass too.为了好玩,你可以尝试expect(stub).to.have.been.platypus ,它也会通过。 Chai's expect interface tolerates nonsensical identifiers. Chai 的expect接口容忍无意义的标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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