Tag Archives: unit test

Unit testing $scope.$on(‘$viewContentLoaded’) on an Angularjs Controller

TL;DR = call

$scope.$broadcast('$viewContentLoaded');

in your unit test. This will call your

$scope.$on('$viewContentLoaded', function () {}); 

method in your controller

If you have a Controller that has a method that is not fired until the view is loaded you will have a method similar to the following:

    $scope.$on('$viewContentLoaded', function () {
       SomeService.callMethod(params);
    });

The test case needs to wire in the $scope into your test like below

describe('when the campaign controller is initialized', function () {

 beforeEach( function() {
    $scope.$broadcast('$viewContentLoaded');
 } );

 it('should call the getAllCampaignTypes service call', function () {
     expect(mockedSomeService.callMethods).toHaveBeenCalledWith( ... );
 });

The Most important thing is the call to $scope.$broadcast(‘$viewContentLoaded’);

Angularjs-mock testing $http by mocking using httpBackend and flush() not working.

Background

I was writing a unit test for my angularjs app and I ran into the following problem.

I was setting up the test correctly to inject in the $httpBackend to mock out the http calls, but the when I called called the service and then called $httpBackend.flush() it would return an error:

httpBackend no pending request to flush

Which is not what I was expecting.

Other background

When I commented out the $httpBackend.whenGET
The following 2 methods in the afterEach function would not throw an error either.

$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();

This would pass the test even though the expectations were never triggered.

Here is an example of the code I was testing with.

Continue reading

Unit testing angularjs with $http

Angularjs unit testing with $http.

How it all hangs together

When calling external servers to get or push data you can use the $http from angular.  $http has a set of methods like Get, Post and Put like you would expect.

$http callback methods.

It treats the call much like a ajax call where it has a .success method and a .error method which it returns to depending on if the call success or failure.

Scoping of parameters and .success() and .errror()

You can interact with the $scope or $rootScope to set or change data.  The scoping of the .success() and .error() means you cannot use a method level parameter and expect the value to be assigned before the the method ends.  It can go out of scope before it is guaranteed to be set.

Continue reading

Angularjs multiple services and Jasminejs unit testing a beginners guide

Angularjs using multiple services and Unit testing services with jasminejs

(update: 29/01/12) I have moved away from angular.module(‘ServiceName’, value( 
and instead use myServices.factory(‘ServiceName’, function(){ as I found  it did not do as I expected to.

Services are a bit harder to unit test and get configured than controllers.
Most examples have a single service or 2 services in a single file.
This is harder to maintain with source control as multiple people could be making changes on the same file.  Here is how to set up and use services in different files.

I have also included some example code on how to Unit test your angularjs service with jasperjs.

Continue reading

Unit testing Grails 2.0 Controllers withForm to handle duplicate submits

The documentation is a bit out of date for Grails 2.0.0 for testing controllers with the withForm { } closure to stop double submits.
New in Grails 2.0.0 is that it is more strict with its unit testing when using the withForm method
Add the following in to allow the form token to allow your method to get into the withForm{} block of your code

setup(){
    def serverUrl = "http://localhost:80"
    org.codehaus.groovy.grails.web.servlet.mvc.SynchronizerTokensHolder
    tokenHolder = SynchronizerTokensHolder.store(session)
    def token = tokenHolder.generateToken(serverUrl)
    params[SynchronizerTokensHolder.TOKEN_KEY] = token
    params[SynchronizerTokensHolder.TOKEN_URI] = serverUrl
}