AngularJS


Unit tests All Versions

0.9.0
0.9.1
0.9.2
0.9.3
0.9.4
0.9.5
0.9.6
0.9.7
0.9.9
0.9.10
0.9.11
0.9.12
0.9.13
0.9.14
0.9.15
0.9.16
0.9.17
0.9.18
0.9.19
0.10.0
0.10.1
0.10.2
0.10.3
0.10.4
0.10.5
0.10.6
1.0.0rc1
g3-v1.0.0rc1
g3-v1.0.0-rc2
v1.0.0rc2
v1.0.0rc3
v1.0.0rc4
v1.0.0rc5
v1.0.0rc6
v1.0.0rc7
v1.0.0rc8
v1.0.0rc9
v1.0.0rc10
v1.0.0rc11
v1.0.0rc12
1.0.0
1.0.1
1.0.2
1.1.0
1.0.3
1.1.1
1.0.4
1.1.2
1.0.5
1.1.3
1.0.6
1.1.4
1.0.7
1.1.5
1.2.0rc1
1.0.8
1.2.0-rc.2
1.2.0-rc.3
1.2.0
1.2.1
1.2.2
1.2.3
1.2.4
1.2.5
1.2.6
1.2.7
1.2.8
1.2.9
1.2.10
1.2.11
1.2.12
1.2.13
1.2.14
1.3.0-beta.1
1.3.0-beta.2
1.3.0-beta.3
1.2.15
1.3.0-beta.4
1.2.16
1.3.0-beta.5
1.3.0-beta.6
1.3.0-beta.7
1.3.0-beta.8
1.3.0-beta.9
1.3.0-beta.10
1.2.17
1.3.0-beta.11
1.2.18
1.3.0-beta.12
1.3.0-beta.13
1.2.19
1.3.0-beta.14
1.2.20
1.3.0-beta.15
1.3.0-beta.16
1.2.21
1.3.0-beta.17
1.2.22
1.3.0-beta.18
1.2.23
1.3.0-beta.19
1.3.0-rc.0
1.2.24
1.3.0-rc.1
1.2.25
1.3.0-rc.2
1.3.0-rc.3
1.3.0-rc.4
1.2.26
1.3.0-rc.5
1.3.0
1.3.1
1.3.2
1.3.3
1.2.27
1.3.4
1.3.5
1.3.6
1.3.7
1.2.28
1.3.8
1.4.0-beta.0
1.3.9
1.3.10
1.4.0-beta.1
1.3.11
1.4.0-beta.2
1.3.12
1.4.0-beta.3
1.3.13
1.4.0-beta.4
1.3.14
1.4.0-beta.5
1.3.15
1.4.0-beta.6
1.4.0-rc.0
1.4.0-rc.1
1.4.0-rc.2
1.4.0
1.3.16
1.4.1
1.3.17
1.4.2
1.4.3
1.4.4
1.3.18
1.4.5
1.3.19
1.4.6
1.5.0-beta.0
1.2.29
1.3.20
1.4.7
1.5.0-beta.1
1.5.0-beta.2
1.4.8
1.5.0-rc.0
1.5.0-rc.1
1.4.9
1.5.0-rc.2
1.5.0
1.4.10
1.5.1
1.5.2
1.5.3
1.5.4
1.5.5
1.4.11
1.5.6
1.4.12
1.5.7
1.2.30
1.5.8
1.5.9
1.6.0
1.6.1

This draft deletes the entire topic.

Examples

  • 16

    Component code:

    angular.module('myModule', []).component('myComponent', {
      bindings: {
        myValue: '<'
      },
      controller: function(MyService) {
        this.service = MyService;
        this.componentMethod = function() {
          return 2;
        };
      }
    });
    

    The test:

    describe('myComponent', function() {
      var component;
    
      var MyServiceFake = jasmine.createSpyObj(['serviceMethod']);
    
      beforeEach(function() {
        module('myModule');
        inject(function($componentController) {
          // 1st - component name, 2nd - controller injections, 3rd - bindings
          component = $componentController('myComponent', {
            MyService: MyServiceFake
          }, {
            myValue: 3
          });
        });
      });
    
      /** Here you test the injector. Useless. */
    
      it('injects the binding', function() {
        expect(component.myValue).toBe(3);
      });
    
      it('has some cool behavior', function() {
        expect(component.componentMethod()).toBe(2);
      });
    });
    

    Run!

  • 9

    Filter code:

    angular.module('myModule', []).filter('multiplier', function() {
      return function(number, multiplier) {
        if (!angular.isNumber(number)) {
          throw new Error(number + " is not a number!");
        }
        if (!multiplier) {
          multiplier = 2;
        }
        return number * multiplier;
      }
    });
    

    The test:

    describe('multiplierFilter', function() {
      var filter;
    
      beforeEach(function() {
        module('myModule');
        inject(function(multiplierFilter) {
          filter = multiplierFilter;
        });
      });
    
      it('multiply by 2 by default', function() {
        expect(filter(2)).toBe(4);
        expect(filter(3)).toBe(6);
      });
    
      it('allow to specify custom multiplier', function() {
        expect(filter(2, 4)).toBe(8);
      });
    
      it('throws error on invalid input', function() {
        expect(function() {
          filter(null);
        }).toThrow();
      });
    });
    

    Run!

    Remark: In the inject call in the test, your filter needs to be specified by its name + Filter. The cause for this is that whenever you register a filter for your module, Angular register it with a Filter appended to its name.

  • 7

    Service Code

    angular.module('myModule', [])
      .service('myService', function() {
        this.doSomething = function(someNumber) {
          return someNumber + 2;
        }
      });
    

    The test

    describe('myService', function() {
      var myService;
      beforeEach(function() {
        module('myModule');
        inject(function(_myService_) {
          myService = _myService_;
        });
      });
      it('should increment `num` by 2', function() {
        var result = myService.doSomething(4);
        expect(result).toEqual(6);
      });
    });
    

    Run!

  • 4

    Controller code:

    angular.module('myModule', [])
      .controller('myController', function($scope) {
        $scope.num = 2;
        $scope.doSomething = function() {
          $scope.num += 2;
        }
      });
    

    The test:

    describe('myController', function() {
      var $scope;
      beforeEach(function() {
        module('myModule');
        inject(function($controller, $rootScope) {
          $scope = $rootScope.$new();
          $controller('myController', {
            '$scope': $scope
          })
        });
      });
      it('should increment `num` by 2', function() {
        expect($scope.num).toEqual(2);
        $scope.doSomething();
        expect($scope.num).toEqual(4);
      });
    });
    

    Run!

  • 3

    Directive code

    angular.module('myModule', [])
      .directive('myDirective', function() {
        return {
          template: '<div>{{greeting}} {{name}}!</div>',
          scope: {
            name: '=',
            greeting: '@'
          }
        };
      });
    

    The test

    describe('myDirective', function() {
      var element, scope;
      beforeEach(function() {
        module('myModule');
        inject(function($compile, $rootScope) {
          scope = $rootScope.$new();
          element = angular.element("<my-directive name='name' greeting='Hello'></my-directive>");
          $compile(element)(scope);
          /* PLEASE NEVER USE scope.$digest(). scope.$apply use a protection to avoid to run a digest loop when there is already one, so, use scope.$apply() instead. */
          scope.$apply();
        })
      });
    
      it('has the text attribute injected', function() {
        expect(element.html()).toContain('Hello');
      });
    
      it('should have proper message after scope change', function() {
        scope.name = 'John';
        scope.$apply();
        expect(element.html()).toContain("John");
        scope.name = 'Alice';
        expect(element.html()).toContain("John");
        scope.$apply();
        expect(element.html()).toContain("Alice");
      });
    });
    

    Run!

Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

This topic provides examples for unit testing the various constructs in AngularJS. Unit tests are often written using using Jasmine, a popular behavior driven testing framework. When unit testing angular constructs, you will need to include ngMock as a dependency when running the unit tests.

Still have a question about Unit tests? Ask Question

Topic Outline