Forward Advance Learning

Introducing Jasmine and Unit testing

Unit testing is a process where we test individual parts of an application individually to make sure they actually do what we think they ought to do. Writing unit tests is important for many reasons:

  1. It gives us confidence that we have done out jobs properly. We can go to bed happy in the knowledge of a job well done.
  2. It helps us when it comes time to refactor.
  3. It helps us upgrade libraries, so we're not stuck on an ancient version of Angular forever.
  4. It acts as documentation for other developers
  5. It helps us become better coders.

Unit testing makes us into better coders

This might sound strange, but it is true. Untestable code tends to look like this:

  • Long, complicated functions that do a lot of things
  • Deep, convoluted dependencies
  • Poorly thought through APIs
  • Unparameterised functions with no clear input and output

This is also coincidentally what bad code looks like.

Testable code on the other hand looks like:

  • Short, single purpose, well named functions that do one thing.
  • Flat, injected dependencies.
  • Well thought through, logical APIs
  • Parameterised functions that receive input, and do something.

Unit testing forces us to use our code as we go. If we can't test a piece of code, it's a strong sign that we need to go home and have a rethink.

Jasmine

Jasmine is an English-like syntax for writing unit tests. Karma supports several syntaxes, but most people use Jasmine style syntax. Jasmine looks like this:

it( 'should add one and one', () => {
expect( 1 + 1 ).toEqual( 2 )
});

In theory, Jasmine tests can be read and understood by a layperson. Jasmine tests should be simple, legible, standard. A new coder should be able to join a team and start reading and writing tests straight away.

Don't go over-complicating your tests with anything surprising or you have defeated the point of testing. Sensible, well named helper functions are allowed.

Exercise - Check that basic maths works

In this exercise, we're going to set up and run some tests in a simple, browser based test harness. In the next section we will automate these tests so they run automatically.

Download the test harness from the Github repository. Open it in a browser. You should see a space where you can put your tests.

Let's play with Jasmine

You'll find in your exercise folder a test harness with an index file which you can open in your browser, and a piece of code called maths.js. This exposes a global object called maths. We can run code like:

maths.sum(2, 2)

and it will return the result.

This code is broken. It's down to you to fix it.

Write code to test and debug maths.sum, maths.power and maths.div. Remember to test edge cases, particularly test with bad data.

Now use tests to develop subtract and exponent functions. Remember to test edge cases.

You can view the Jasmine documentation here: http://jasmine.github.io/2.1/introduction.html

Exercise - Escape the Dungeon

You will find a starting point in the exercises folder. Write specs for the models to ensure they do what you expect them to.