Throw Error newb question.

20 views
Skip to first unread message

Rodrigo

unread,
Sep 4, 2016, 2:35:00 PM9/4/16
to Jasmine
Hi,

I'm just starting with Jasmine and I'm having a bit of a hard time understanding how throwError works.

In the official docs this is the sample code:

foo = {
  setBar: function(value) {
   
bar = value;
  }

spyOn(foo, "setBar").and.throwError("quux");

And then comes the expectation for the spec:

expect(function() {
  foo.setBar(123)
}).toThrowError("quux");

What I can't seem to understand is, if the throwErrror is being added to the spy and then the setBar method is being called, When can we expect that the setBar method is not going to throw an error "quux"?. As far as I can understand the setBar method will always throw an error. I tried creating a method that actually throws an error if no arguments are passed, like this:

var foo = {
  setBar: function(value) {
    if(!value) {
      throw "you must pass a value"
    } else {
      bar = value;
    }
  }
};

// then
it("Should throw error", function(){

  spyOn(foo, "setBar").and.throwError("no value");

  expect( function() {
    foo.setBar()
  }).toThrowError("no value");

});

I would expect the error "you must pass a value", but it always throws the error passed in  and.throwError().

As a follow up question, what's the point of testing an error that's already defined and will be returned everytime no matter what arguments we use in the function?.

I can't understand the need of this when there's already the toThrowError for a simple call:

var foo = {
  setBar: function(value) {
    if(!value) {
      throw "you must pass a value"
    } else {
      bar = value;
    }
  }
};

it("Should throw error", function(){

  expect( function() {
    foo.setBar()
  }).toThrowError("you must pass a value");

});

I'm pretty sure I'm missing something about spies, but after reading and reading I can't seem to find what.

Thanks,
Rodrigo.

Gregg Van Hove

unread,
Sep 4, 2016, 9:09:02 PM9/4/16
to jasmi...@googlegroups.com
The example in the docs is contrived and definitely not something you would want to do on your actual specs. You may notice at the bottom of the page, that the specs defined in that par actually run, and the current goal is for them to be green. This is something we're trying to figure out how to solve, so we can get some more comprehensive documentation. 

So, once you spy on a function, you stop the real implementation from being called, unless you call 'callThrough`. The intent of `and.throwError` is to allow testing of error handling code in the code that calls the function throwing the error. 

Hope this helps. Thanks for using Jasmine!

- Gregg 
--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+unsubscribe@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages