Am I using expect().toThrow incorrectly?

2,824 views
Skip to first unread message

Jon Prins

unread,
Nov 16, 2010, 12:48:38 PM11/16/10
to Jasmine
Hi there,

I cannot for the life of me get this test to pass. I may be
using .toThrow improperly... but I'm not sure, as there isn't an
example with a corresponding code snippet. Any help would be much
appreciated!

In edited form:

function myFunctionToTest (parameter) {
/* do some stuff, and if parameter is out of bounds */
throw new TypeError("Invalid parameter")
}


My test, then, looks a little like this:

expect(myFunctionToTest(invalidParamter)).toThrow()

and I've tried just about everything as a parameter to toThrow. new
TypeError. New TypeError("Invalid parameter"). "Error: TypeError:
Invalid parameter." (what chrome logs when you throw the error).

It always fails because of the thrown error - which is what I'm
testing to make sure happens!

Any suggestions on how to do this properly would be much loved :)

Thanks,
Jon

Christian Williams

unread,
Nov 16, 2010, 1:50:04 PM11/16/10
to jasmi...@googlegroups.com
You need to wrap the whole call inside an anonymous function:

> expect(function() {myFunctionToTest(invalidParamter)}).toThrow()

--X [typos courtesy of my iPhone]

> --
> You received this message because you are subscribed to the Google Groups "Jasmine" group.
> To post to this group, send email to jasmi...@googlegroups.com.
> To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.
>

Jonathan Prins

unread,
Nov 16, 2010, 2:45:32 PM11/16/10
to jasmi...@googlegroups.com
Aha! Fantastic, works like a charm.
Reply all
Reply to author
Forward
0 new messages