Inside the callback we use the built in expect method. Our tests usually check boolean conditions. In this case, we test whether true is equal to true. In unit testing, we try to create methods that can be verified with boolean statements. For instance, we can use a boolean statement to confirm that a method called add returns the value 5 when passed 3 and 2.

We frequently also include methods that set up the environment in which our tests run. These set up methods include calls to the Jasmine beforeEach() and afterEach() methods. Those methods are called before and after each test.

In the above code, we use a beforeEach method to ensure that name is set to 'Sally' before each test is run. Even if one of the tests modifies the string, the beforeEach method will reinitialize it to 'Sally'.

Jasmine Server

This section shows how to set Jasmine up to do server side tests. We will discuss setting Jasmine up for the client elsewhere.

Install jasmine globally:

npm install -g jasmine

Create the /spec/support/jasmine.json file. It should contain something like this:

{
"spec_dir": "spec",
"spec_files": [
"**/test*.js"
]
}

This bit of JSON tells Jasmine that our tests are in a directory called spec and that each file that contains tests begins with the word test. For instance: test-basic.js or test-state-parser.js.

Here is an example test suite called test-basic.js to put in your spec directory:

Focused Tests

Use the new focused specs feature from Jasmine 2.1 to work with one suite at a time. To do this, change describe to fdescribe or it to fit. It you make a suite with fdescribe, then just that one suite will run and the others won't clutter your screen:

Looking at the function above, do you see that the test is past done
as a parmater? Notice also that it calls done() when it is finished.
The call to done notifies Jasmine that your callback is complete.

SpyOn

Create a mock object and instrument the object so that you can learn
key facts such as:

First, it gets the scope that you are using in your application. I believe that this is a global scope object for the entire app, and this line narrows the scope down to work only with the scope of your controller:

mainController = _$controller_('MainController', {
$scope: scope
});

The above code also loads your controller.

These line gets the compile and templateCache, both of which are needed to process our fixture so that it is converted from an angular template to live HTML that contains resolved references to scope variables:

The beforeEach shown above is the same as the ones we looked at before, except it does not get the compile and templateCache for handling fixtures, and instead it gets httpBackend. We use httpBackend for mocking calls to the server. Instead of aclled $http.get directly, we use httpBackend calls such as this to mock or fake the call to the server. Instead of making a real call, we just return pre-defined data: