Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to test controllers created with angular.module().controller() in Angular.js using Mocha

I have a controller created with angular.module().controller() like in this situation

myModule = angular.module('myApp.controllers', [])
                   .controller('testCtrl', ['$scope', function($scope){
                           $scope.test = 'this is a test';
                    }]);

now, I need to use mocha to test if my controller is working properly. In Angular there are some examples when controllers are declared as global functions ( ex. http://docs.angularjs.org/tutorial/step_04 ), so they use

function PhoneListCtrl() {...}
.....
beforeEach(function() {
   scope = {},
   ctrl = new PhoneListCtrl(scope);
});

it('shod test whatever PhoneListCtrl does ', function() {
   expect(scope.someProp).toBe('whateverValue');
});    

so the questions are:

1) how can I do a similar test for controllers that are declared using angular.module().controller()

2) how to do it using Mocha

like image 840
Liviu Avatar asked Nov 27 '12 14:11

Liviu


People also ask

What is module and controller in AngularJS?

An AngularJS module defines an application. The module is a container for the different parts of an application. The module is a container for the application controllers. Controllers always belong to a module.

Which function is used to create a module in AngularJS?

module() method creates an application module, where the first parameter is a module name which is same as specified by ng-app directive.

Can you create controller in AngularJS?

AngularJS ControllersAngularJS applications are controlled by controllers. The ng-controller directive defines the application controller. A controller is a JavaScript Object, created by a standard JavaScript object constructor.


1 Answers

AngularJS provides mocks that make available some useful functions for dependency injection while testing.

  • module
  • inject

Example:

(in jasmine)

Let's say we want to perform the first test from the official tutorial and we have defined a controllers module. (you could namespace the module name, but I want to keep it simple)

var Controllers = angular.module('controllers', []);

Controllers.controller('PhoneListCtrl', ['$scope', function($scope){
    $scope.phones = [{name: "Nexus S", snippet: "Fast..."},
                     {name: "Motorola XOOM...", snippet: "The Next...."},
                     {name: "MOTOROLA XOOM...", snippet: "The Next, Next..."}];
}]);

Then we create a module for out app and inject it our controllers module

var PhonesApp = angular.module('phoneApp', ['controllers']);

Finally we can test it like this

describe('phonesApp', function() {
    describe('phoneApp controllers', function() {
        beforeEach(module('controllers'));
        describe('PhoneListCtrl', function() {
            it('should create "phones" model with 3 phones',
                inject(function($rootScope, $controller) {

                var scope = $rootScope.$new();
                var ctrl = $controller("PhoneListCtrl", {$scope: scope });
                expect(scope.phones.length).toBe(3);
            }));
        });
    });
});

I haven't done it in mocha, but I guess the process is similar.

Pd: I did the tutorial using CoffeeScript, here are the relevant bits https://gist.github.com/4163147

like image 57
jaime Avatar answered Oct 06 '22 00:10

jaime