Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing an AngularJS factory with Karma (Jasmine)

I'm struggling with testing an AngularJS factory using Karma + Jasmine.

I am unable to inject my factory to OfficerValidationService variable.

What am i doing wrong?

Note: the file is loaded properly

Factory:

'use strict';

angular.module('darthvader').factory('OfficerValidationService', [function(){

  var OfficerValidationService = {};

  OfficerValidationService.something = function() {
    return true;
  };

  return OfficerValidationService;

}]);

Code:

'use strict';

(function() {
  describe('OfficerValidationService Spec', function() {

    var OfficerValidationService;

    beforeEach(function() {
      angular.module('darthvader');
    });

    beforeEach(module('darthvader', function($provide) {
      $provide.value('OfficerValidationService', OfficerValidationService);
    }));

    it('is very true', inject(function(OfficerValidationService){
      alert('=====' + OfficerValidationService + '=====');
      var output = OfficerValidationService.something();
      expect(output).toBeTruthy();
    }));

  });
}());

Output:

Running "karma:unit" (karma) task
INFO [karma]: Karma v0.12.17 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Mac OS X)]: Connected on socket yc_qTseOCArRtZLAXSTZ with id 97647403
ALERT: '=====undefined====='
PhantomJS 1.9.7 (Mac OS X) OfficerValidationService Spec is very true FAILED
    TypeError: 'undefined' is not an object (evaluating 'OfficerValidationService.something')
        at /Users/chuck/Desktop/sandbox/dashboard.darthvader/test/karma/unit/services/officerValidationService.js:25
        at invoke (/Users/chuck/Desktop/sandbox/dashboard.darthvader/public/lib/angular/angular.js:3917)
        at workFn (/Users/chuck/Desktop/sandbox/dashboard.darthvader/public/lib/angular-mocks/angular-mocks.js:2155)
    undefined
PhantomJS 1.9.7 (Mac OS X): Executed 28 of 28 (1 FAILED) (0.102 secs / 0.131 secs)
like image 633
chuckfinley Avatar asked Jul 28 '14 19:07

chuckfinley


People also ask

Does AngularJS use Jasmine?

Angular ships with Jasmine, a JavaScript framework that enables you to write and execute unit and integration tests. Jasmine consists of three important parts: A library with classes and functions for constructing tests. A test execution engine.

What is Jasmine testing framework and how do you use it for angular unit testing?

Jasmine is a behavior development testing framework. Unit tests are written using Jasmine and are run to see if individual parts of an application are working correctly. As a result, unit tests will either pass or fail depending on if the code is working correctly or has a bug.


2 Answers

You need to call angular.injector:

'use strict';

(function() {
  describe('OfficerValidationService Spec', function() {

    var OfficerValidationService;

    beforeEach(function() {
      angular.module('darthvader');
    });

    beforeEach(inject(function() {
      var $injector = angular.injector(['darthvader']);
      OfficerValidationService = $injector.get('OfficerValidationService');
    }));

    it('is very true', function(){
      var output = OfficerValidationService.something();
      expect(output).toBeTruthy();
    });

  });
}());
like image 95
martynas Avatar answered Sep 28 '22 06:09

martynas


To add to martynas' answer, here is a simplified version:

module() is the shorthand of angular.mock.module
inject() is the shorthand of angular.mock.inject (same as $injector)

'use strict';

(function() {
  describe('OfficerValidationService Spec', function() {
    var OfficerValidationService;

    beforeEach(module('darthvader'));
    beforeEach(inject(function (OfficerValidationService) {
      OfficerValidationService = OfficerValidationService;
    }));

     it('OfficerValidationService should exist', function() {
        expect(OfficerValidationService).toBeTruthy();
    });


  });
}());
like image 38
Jean-Baptiste Bouhier Avatar answered Sep 28 '22 07:09

Jean-Baptiste Bouhier