AngularJS Tutorial - 5

New version Angular 9

Now we should complete our CRUD. But we already have too much functionalities we will take a look at...testing! We will add the Create, Update and Delete in the next step!

Prerequisites for testing

We will create a folder test, in which we will add the "test/specRunner.html" file that will contain the runner for Jasmine a Javascript unit test framework. Its content will be the following.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    <title>Jasmine Spec Runner</title>

    <!-- include Jasmine -->
    <link href="//" type="text/css" rel="stylesheet">
    <script src="//" type="text/javascript"></script>
    <script src="//" type="text/javascript"></script>

    <!-- include angular and all its dependencies -->
    <script type="text/javascript" src=""></script>
    <!-- include angular mocks -->
    <script src="//" type="text/javascript"></script>
    <!-- include angular extra modules -->
    <!-- include source files here... -->
    <!-- include spec files here...-->    
    <script type="text/javascript">
        (function() {
            var jasmineEnv = jasmine.getEnv();
            jasmineEnv.updateInterval = 1000;
            var htmlReporter = new jasmine.HtmlReporter();
            jasmineEnv.specFilter = function(spec) {
                return htmlReporter.specFilter(spec);
            var currentWindowOnload = window.onload;
            window.onload = function() {
                if (currentWindowOnload) {
            function execJasmine() {

We should then add to the file (for our project)

If we run the "test/specRunner.html" nothing will appear, because we have not any test, called Specs by Jasmine. We create then the file "test/spec/app.test.js" and whe add "spec/app.test.js" to our test runner in the "specs" section.

Then we add a simple tet for the existance of the EmptyController. These are the steps: * describe "Contacts Application": Create a categorization of tests (we are testing the ContactsApp!) * beforeEach module('ContactsApp'): Initialize the module "ContactsApp" * describe "EmptyController": Create a categorization of tests (we will test the EmptyController) We are telling that we are creating a category "ContactsApp" that, before running the module "ContactsApp" will run The tet "EmptyController". * it 'Should be declared': The test outcome * inject( $contoller): Inject the controller factory * $controller('EmptyController'): Instantiate the controller EmptyController

describe("ContactsApp Services", function() {
    describe("listController", function() {
        it('listController should be declared',
           inject(function(listController) {

Now running the page will show the tests results!

Mocking, the Repository

Mock Object: according to Wikipedia, In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. A programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behavior of a human in vehicle impacts

We will create a mock for the repository. We want to test our list service in isolation and we want simply to know if it interacts correctly with the rest of the environment. We will add a "test/lib/mock.js" file in wich we will store the mocks. And we add the reference to the specRunner.

function buildMockRepository(dataToReturn = null,returnsFunction = null){
        returnsFunction = function(){
                success: function(doCallback){
                    var data = {
    return {
        calledMethod : "",
        calledObject : null,
        getAll : function() {
            this.calledMethod = "getAll";
            this.calledObject = null;
            return returnsFunction();
        getById : function(id) {
            this.calledMethod = "getById";
            this.calledObject = id;
            return returnsFunction();

The Repository Mock takes two parameters, a callback function to get the data, that will return usally an "ok" message without data, as our api does; and a "dataToReturn" field, that will contain the data eventually sent back (just when we need it to get test consistency.

With this, the two functions that will be mocked store the method called and the parameter passed to the mock.

Testing the listController

Now to use the mock we should first initialize the mock, and then a listController to use for all our request, we will add after the "listController should be required" the construction of the mock and the initialization of the test environment.

We create a new scope that will be used for subsequent tests and through the $controller service we will retrieve the 'listController' instance

    var repositoryService  = buildMockRepository();
    var listController;
    var scope;
    beforeEach(inject(function($rootScope, $controller) {
        scope = $rootScope.$new();
        listController = $controller('listController',{

First we verify that the controller actually had been declared

    it('listController should be declared',function() {

Now we can use the listController just declared and verify that the simple istantiation would be calling "getAll" and that no parameters had been passed!

    it('listController should call the repository getAll',function(){

Finally we check that the variable "scope.test" exists on the scope.

    it('listController should set the result variable on the scope',function(){

Mocking the UI-Bootstrap dialog

Now, to test the detailController we must mock the $modal service, this can be done pretty easily (in this context!! search for this on google and the Challenger Deep opens suddenly..). This mock will be placed too in the mock.js. Note the trick on the mockModalInstance to change the variable inside the instance!!

function buildMockModal(){
    var modal = {
        mockModalInstance : {
            close: function(param){
                modal.closeCalled = true;
                modal.closeParam = param;
                    modal.thenCalled = true;
        callBack : null,
        command: null,
        open: function(param) {
            this.openCalled = true;
            this.openParam = param;
            return {
                result: {
                    then: function(callback){
                        this.callBack = callback;
    modal.mockModalInstance.modal = modal;
    return modal;

This mock signal the calling of the open, close functions and their parameters. Other than this it simulate the "then" javscript promise

Testing the detailController

Now, we could finally test the detail controller. First we should initialize the test. We are using a repository that returns an object with a specified id. We create too the mocked modal.

    var id = 'testId';
    var repositoryService  = buildMockRepository({
            id : id
    var detailController;
    var mockModal = buildMockModal();

We should now instantiate the detailController, and test for its existence

    beforeEach(inject(function($rootScope, $controller) {
        scope = $rootScope.$new();
        detailController = $controller('detailController',{
    it('detailController should be declared',function() {

The only function present will be "open", we then verify that it's defined on the scope, that the repository had been called and that the dialog had been opened with the correct parameters.

    it("open function should be added to scope", function() {
    it('open function should call the repository getById',function(){;
    it('open function should call the $modal open',function(){;
    it('open function should call the $modal with the correct parameters',function(){;

Testing the ContactsRepositoryService

We add a new "test/spec/contact.test.js" to the specRunner. And setup a new block of tests. Plus the one for our repository service. Note the usage of the $httpBackend, this is the wrapper that is used by angular-mocks for the $http service

describe("ContactsApp-Contact Module", function() {
    describe("ContactsRepositoryService", function() {
        var contactsRepositoryService;
        beforeEach(inject(function(ContactsRepositoryService, _$httpBackend_){
            contactsRepositoryService = ContactsRepositoryService;
            $httpBackend = _$httpBackend_;

We could then do our verifications on the two methods "getAll" and "getById". The response is added to verify that the correct mocked http request had been called.

    it("getAll should make an ajax call to api/contacts/", function () {
            result: "ok",
            data :{}
    it("getById should make an ajax call to api/contacts/?id=objId", function () {
            result: "ok",
            data :{id:"objId"}


Download the sample source

Last modified on: September 10, 2014