Angular module dependency failure in Jasmine unit tests

I am trying to execute the unit test code of a controller inside a module that uses other modules as dependencies, but could not figure out how to simulate them correctly.

I use the Jasmine Framework and run my tests using Karma (Testacular).

Module code

var app = angular.module('events', ['af.widgets', 'angular-table']); app.controller('eventsCtrl', function([dependencies]){ $scope.events = []; ... }); 

Specification code

 describe('events module', function(){ var $scope, ctrl; beforeEach(function(){ angular.mock.module('af.widgets', []); angular.mock.module('angular-table', []); module('events', ['af.widgets', 'angular-table']); }); beforeEach(inject(function($rootScope, $controller){ $scope = $rootScope.new(); ctrl = $controller('NameCtrl', { $scope: $scope, }); })); it('should have an empty events array', function(){ expect($scope.events).toBe([]); }) }); 

The error I get is "there is no af.widgets module", so obviously I am not mocking module dependencies. Any hints?

+69
angularjs unit-testing karma-runner jasmine
Jul 09 '13 at 17:38
source share
3 answers

If you want to make fun of a module declaring one or more services, I used this code:

 beforeEach(function(){ module('moduleToMock'); module(function ($provide) { $provide.value('yourService', serviceMock); }); }); 

This is useful if the service you want to make fun of is also the service you want to use unit test (in another description of jasmine). The solution suggested by fscof is great, but you cannot create unit test for the angular-table module.

+60
Aug 14 '13 at 10:07 on
source share

Here is what I understood:

I did not load the 'angular -table' modules in the karma.conf.js file, so there is an error. This was intentional at first, as I wanted to test the "events" module without the actual table module.

I was able to easily make fun of the angular-table 'module by creating a new file in the test folder named "mocks / angular -table.js" and added the following code:

/ scoffing / angular -table.js

 'use-strict'; angular.module('angular-table', []); 

I added this file to my karma.conf.js file along with a real events module that I wanted to check:

karma.conf.js

 ... files = [ JASMINE, JASMINE_ADAPTER, 'scripts/libs/angular.js', 'scripts/libs/angular-mocks.js', 'scripts/events.js', // this is the real module. 'scripts/mocks/*.js', //loads all custom mocks. 'scripts/specs/*.spec.js' // loads my spec file. ] ... 

Finally, in my spec file, I was able to add both modules by calling them separately in the beforeEach block:

specs / events.spec.js

 beforeEach(function(){ module('angular-table'); module('events'); }); 

I got the idea to structure my files this way from this post

+44
Jul 09 '13 at 19:38
source share

I recently released ngImprovedTesting, which should make mock testing in AngularJS mode easier.

In your case, just use the following in your Jasmine test: beforeEach (ModuleBuilder.forModule ('events') serviceWithMocks ('eventsCtrl') build ().).

For more information about ngImprovedTesting, check out your opening blog post: http://blog.jdriven.com/2014/07/ng-improved-testing-mock-testing-for-angularjs-made-easy/

+3
Jul 29 '14 at 9:38
source share



All Articles