简体   繁体   中英

How can I expect for a failure in a Mocha test?

I am testing a piece of code where I want to specifically test that a certain event is never triggered.

      eventBus.once("property:change", function(msg) {            
        expect(true).to.eq(false);
        done();
      });

Instead of 'expect(true).to.eq(false);' or 'done(new Error("should have never been reached"));' is there a way to say

     fail("should have never been reached"):

The latter would be much more expressive. Is there a statement/solution like this, couldn't find any.

I would use a spy - http://sinonjs.org/

var callback = sinon.spy();
eventBus.once("property:change", callback);

// Things that could potentially but should not trigger the event

assert.equals(callback.callCount, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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