Testing for Exceptions with Jasmine
- Estimated read time: 5 min read
- Written by Chad Campbell on Jan 18th 2014
toThrowError() functions as an elegant alternative.
As you might expect, the
Scenario #1: Testing a basic function that should throw an exception
This function is fairly plain. It has a name. Yet, it does not take any parameters. Now, imagine you want to test this function. To test with Jasmine, you could do the following:
This test uses Jasmine's behavior-driven development (BDD) style of testing. Take note of the fact that we referenced the name of the function that's being tested instead of calling it. In other words,
myProcedure was used instead of
myProcedure() (note the absence of parenthesis). If you include the parenthsis, the test results would show an error that says something like:
Not very descriptive. It is what it is though. Still, using the syntax above may raise another question. You might be wondering, how do I test a function that expects parameters?
Scenario #2: Testing a function that expects parameters
Pretend for a minute that you need to test a function that accepts parameters. The purpose of the function is to generate a random number and prefix it with a message. A function that does that may look like the following:
The function above takes two parameters:
msg. With a function of this size, there are a few things to test. However, let's start by ensuring we can pass parameters to the function from a test. To create a test that passes parameters to a function, you need to do something like this:
The test above tests the function that was previously defined and passes parameters to it. The parameters are passed with the help of the
bind function. This function was introduced with ECMAScript 5. It basically creates a wrapper function so that you don't have to manually create an anonymous function. The context is maintained via the first parameter, which will usually be set to
this. After you've passed that parameter in, you're free to add the parameters your function is expecting.
As more parameters are added, its more likely that there will be other things to test. With more things to test, there's a growing chance that you may return different types of exceptions to describe the problem. For that reason we'll look at a third scenario where different types of exceptions are involved.
Scenario #3: Testing a function that uses different types of exceptions
myOtherProcedure made use of two types of exceptions:
With Jasmine, we can still test code that makes use of custom exceptions. To help with that task, Jasmine has a new sibling to
toThrowError. A test using that function is shown here:
In the above example, the name of the exception type that's expected is passed to
Error object. This frees you up to test all kinds of exceptions with Jasmine.
This blog post showed you three ways to test for exceptions with Jasmine. You can view the demo or download the code. For the purposes of this blog post, Jasmine 2.0 was used. If you have any questions or comments related to this post, please leave them in the comments below. If you found this post helpful, I hope you will share this post via Google Plus, LinkedIn, or Twitter.