Abilash
Abilash

Reputation: 6089

Problems Setting Up Testacular With Jasmine and Angular

I'm trying to set up a unit testing environment for my project and I'm having some problems. I'm trying to use Testacular with Jasmine for testing my AngularJS code.

I have a module Services which has a factory method called KeepAlive. Here is its set up.

angular.module('services', []).factory('KeepAlive', ['$rootScope', function($rootScope){
    //My Code
}]);

This module is attached to a module called MainModule. Here is its code.

angular.module('MainModule', ['filters', 'services', 'directives', 'ui'])
.config(['$httpProvider', function($httpProvider){
    //My Code
}]).run(['$rootScope', '$timeout', '$routeParams', 'KeepAlive', function($rootScope, $timeout, $routeParams, KeepAlive){
    //My Code
}]);

So Now I want to add this code for my tests. So here is my testacular file set up.

     JASMINE,
     JASMINE_ADAPTER,
     'assets/lib/jquery/jquery-1.7.1.js',
     'assets/lib/angular/angular.js',
     'assets/lib/angular/angular-ui.js',
     'jsTests/MockingLib/angular-mocks.js',
     'assets/scripts/modules/Admin.js',
     'assets/scripts/modules/MainModule.js',
     'assets/scripts/services/KeepAlive.js',
     'assets/scripts/services/Admin.js',
     'assets/scripts/filters.js',
     'assets/scripts/directives.js',
     'assets/scripts/controllers/admin/*.js',

Now this is throwing a error in my testacular console. Here is the error that I'm getting.

Error: Unknown provider: KeepAliveProvider <- KeepAlive
     at Error (<anonymous>)
     at d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2627:8
     at Object.getService [as get] (d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2755:32)
     at d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2632:38
     at getService (d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2755:32)
     at Object.invoke (d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2773:6)
     at d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2637:71
     at Array.forEach (native)
     at forEach (d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:110:11)
     at Object.createInjector [as injector] (d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2637:3)

Here is my Jasmine Test case.

beforeEach(function () {
    admin = module("MainModule");
    module("services");
});

What I'm I missing? I'm new to testacular. Thanks in advance.

Upvotes: 0

Views: 932

Answers (1)

Vojta
Vojta

Reputation: 23051

The error says that somebody asked for a service called "KeepAlive" but the injector does not know how to create it (there is no provider for it).

That means, you probably didn't load the module.

When you bootstrap your app on production, you give it ng-app="something", which tells Angular which module to load. In the same way, you need to specify which modules you wanna load during testing.

This should solve your problem:

beforeEach(module('services'));

Upvotes: 4

Related Questions