Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a failure message to expect calls

I'm currently learning AngularJS and part of that covers creating tests. At the moment I'm trying to work out how to create more useful error messages for failing tests. For example, if I was in Java-land and writing JUnit tests I would do something akin to this:

assertTrue( "The foo value should be true at this point", bar.isFoo() );

That way, I'll get that first parameter in the log if the check fails.

For boolean checks in mocha (with chai and sinon, in case that makes a difference) I have...

expect(broadcastSpy.calledWith('order.add')).to.be.true;

If that fails then I get the following:

expected false to be true
AssertionError: expected false to be true

Is there a way to replicate that helpful failure message while testing my app?

like image 739
chooban Avatar asked May 20 '14 14:05

chooban


1 Answers

I mentioned something about this in an answer to a different question. I've just checked Chai's code to be sure that nothing has changed since I wrote that answer and found the following is still true. The code:

expect(foo).to.be.true;

does not accept a custom message because true is a property that obtains its value through a getter. You can, however, do this:

expect(foo).to.equal(true, "foo should be true");

to get a custom message if the assertion fails.

Chai's assert interface supports messages on all assertions, for instance:

assert.isTrue(foo, "foo should be true");
like image 191
Louis Avatar answered Sep 30 '22 22:09

Louis