Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark a jasmine test as failed?

I have a jasmine 2.0 test that if a function is called, the test failed.

I have a function "Remote.get" that should call the first argument (which is a callback) if it is successful, or the second argument if it failed.

If it calls the second argument, I need to mark the test as failed.

How can I clearly mark the test as failed?

describe("my tests", function() {
  it("should call the first function", function(done) {          
    Remote.get(
      function() {
        // yeah! good!
        done();
      },
      function() {

        // whoa, if we got here, then it didn't work
        // fail()!

        done();
      }
    );             
  });
});

I know I could do something like expect(true).toBe(false) but I the error you get then would be unclear and unrelated to the actual problem. It should give an error like "wrong callback was called" or "Remote.get failure was called". I was hoping there was something more descriptive in Jasmine.

What I'm really looking for is the python equivalent of http://docs.python.org/2/library/unittest.html#unittest.TestCase.fail.

like image 297
leech Avatar asked Feb 26 '14 17:02

leech


2 Answers

They added a fail() method very recently in this commit. It will likely be released with the next point release of jasmine (assuming 2.1, unclear when this will be), or you can build your own from edge.

like image 148
mmocny Avatar answered Sep 22 '22 02:09

mmocny


You could write and register a custom matcher whose compare function in the return value always fails, and assign your custom message to the message property

like image 45
ossek Avatar answered Sep 26 '22 02:09

ossek