I've seen this answered but none of the solutions are working for me. I'm getting an error Error: [ng:areq] Argument 'fn' is not a function, got undefined which is causing my test to fail. What's the correct way to inject a factory into a test spec that isn't a stubbed factory.
login module
angular.module('loginModule', ['ui.bootstrap', 'permission', 'ui.router', 'coreModule', 'utilsModule']);
login controller
(function(){
'use strict';
angular.module('loginModule')
.controller('loginController', login);
login.$inject = [
'$log',
'$uibModal',
'$rootScope',
'storageFactory',
'loginFactory',
'$state',
'RoleStore',
'PermissionStore',
'vsmsCoreFactory'
];
function login($log, $uibModal, $rootScope, storageFactory, loginFactory, $state, RoleStore, PermissionStore, vsmsCoreFactory) {
/* jshint validthis: true */
var vm = this;
vm.loginUser = loginUser;
vm.forgotPassword = forgotPassword;
vm.errorCode = null;
PermissionStore.clearStore();
vsmsCoreFactory.getHashFunction()
.then(function(response) {
if(response) {
storageFactory.setHashFunction(response); // unknown provider
}
});
function loginUser() {
...
login controller spec
describe('loginController', function() {
var $controller;
beforeEach(module('loginModule'));
beforeEach(inject(function(_$controller_){
$controller = _$controller_;
}));
describe('vm.loginUser', function() {
it('should be defined', function() {
var loginController = $controller('loginController');
expect(loginController.loginUser).toBeDefined();
});
});
});
unit test files
'use strict';
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var wiredep = require('wiredep');
var paths = gulp.paths;
function runTests (singleRun, done) {
var bowerDeps = wiredep({
directory: 'bower_components',
exclude: ['bootstrap-sass-official'],
dependencies: true,
devDependencies: true
});
var testFiles = bowerDeps.js.concat([
'./src/components/scripts/ui-bootstrap-custom-tpls-2.1.3.js',
'./src/app/index.js',
'./src/{app,components}/**/*.module.js',
'./src/{app,components}/**/*.factory.js',
'./src/{app,components}/**/*.controller.js',
'./src/{app,components}/**/*.spec.js'
]);
gulp.src(testFiles)
.pipe($.karma({
configFile: 'karma.conf.js',
action: (singleRun)? 'run': 'watch'
}))
.on('error', function (err) {
// Make sure failed tests cause gulp to exit non-zero
throw err;
});
}
gulp.task('test', function (done) { runTests(true /* singleRun */, done) });
gulp.task('test:auto', function (done) { runTests(false /* singleRun */, done) });
app
'use strict';
angular.module('fotaAdminPortal',
[
'ngAnimate',
'ngCookies',
'ngTouch',
'ngSanitize',
'ngResource',
'ui.bootstrap',
'ui.router',
'ui.router.stateHelper',
'pascalprecht.translate',
'utilsModule',
'loginModule',
...
])
log
Chrome 54.0.2840 (Mac OS X 10.11.6) loginController vm.loginUser should be defined FAILED
Error: [$injector:unpr] Unknown provider: storageFactoryProvider <- storageFactory <- loginController
The fotaAdminPortal module isn't loaded for the test so vsmsCoreFactory never gets registered.
Also there's no need to manually pass services into a controller as the injector will automatically do this when you create it. Passing services manually is useful when you need to pass something that isn't a globally registered service ie $scope or mock a service for a single controller instance.
Atm the mocks for the factories are just an undefined variable which will cause errors when the logic depending on them tries to call the methods that don't exist. You'll need to properly stub any methods on these mocks. But leaving those aside for the moment your test should be something like:
describe('loginController', function() {
var $controller;
beforeEach(module('fotaAdminPortal'));
beforeEach(inject(function(_$controller_){
$controller = _$controller_;
}));
describe('vm.loginUser', function() {
it('should be defined', function() {
var loginController = $controller('loginController');
expect(loginController.loginUser).toBeDefined();
});
});
});
If you want to load loginModule in isolation then you need to extract vsmsCoreFactory into another module and add that as a dependency:
angular.module('vsmsCore', [])
.factory('vsmsCoreFactory', function($http, env, $log, storageFactory) {
});
angular.module('loginModule', ['ui.bootstrap', 'permission', 'ui.router', 'vsmsCore']);
Then you can do beforeEach(module('loginModule')) instead
Related
I'm getting the error: "[$injector:modulerr] Failed to instantiate module myApp due to: ReferenceError: isUndefined is not defined". It suggests I misspelled something or forgot to included dependencies. I've checked and all my scripts are included, all my spelling is correct, and I don't think I've left any dependencies out. I've got a lot of other controllers structured very similarly to this one with no problem, but now two of my most recent added controllers/views are giving me the error. I stripped out all the logic to make sure I wasn't doing something inadvertent, but still no dice. I'm including the module it says is undefined, a similar controller that works, and the controller (now stripped of all logic except a console.log) for the new view that doesn't work. Any ideas?
The "undefined" module (with a number of logic taken out for brevity):
var app = angular.module("myApp", ["ngRoute", 'ngAnimate', 'angular-growl', 'ui.select', 'ngSanitize', 'ui.bootstrap', 'ui.bootstrap.datetimepicker']);
var HeaderCtl = function($scope, $location) {
console.log('inside HeaderCtl : ' + $scope.isLoggedIn);
$scope.tabSelected = function(tabname) {
console.log('Server Parameter :' + $scope.tabname);
$location.path("/" + $scope.tabname);
}
}
app.controller("HeaderCtl", ["$scope", "$location", 'growl', HeaderCtl]);
The similar working controller based on the module:
(function() {
var app = angular.module("myApp");
var loginController = function($scope, $rootScope, $http, $location, myApp, growl) {
// $scope.currenttab = $scope.isLoggedIn;
$scope.sendDt = function() {
$http.post('/login', {
username: $scope.user.username,
password: $scope.user.password,
})
.success(function(user) {
$rootScope.isLoggedIn = 'yes';
console.log('successfully logged in and redirecting to :');
$location.path("/");
$http.get('/getnavlist').success(function(list) {
if (list !== '0') { // got some menus
$rootScope.navlist = list;
$location.path("/" + list[0].value);
}
});
},
function() {
console.log('error in logging in');
$rootScope.message = 'user name or password is wrong';
$location.path("#/welcomeLogin");
});
}
};
app.controller("loginController", loginController);
}());
Finally, the error module with logic stripped out:
(function() {
var app = angular.module("myApp");
var signUpController = function($scope) {
$scope.formModel = {};
console.log("Woomp!");
}
app.controller("signUpController", signUpController);
}());
You can't inject angular module as injectable inside factory function. In loginController you had injected myApp(module name) as a dependency. This is root cause of your problem.
Change
//VVVVV//remove this
var loginController = function($scope, $rootScope, $http, $location, myApp, growl) {
to
var loginController = function($scope, $rootScope, $http, $location, growl) {
I want use unit test in angular, but the webapp that I working on it is created on its owe structure. for example :
(function() {
'use strict';
angular
.module('app', [
'ngAnimate',
'ui.load',
'ui.jp',
'oc.lazyLoad'
]);})();
And one of its controllers :
(function() {
'use strict';
angular
.module('app')
.controller('EditorCtrl', EditorCtrl);
function EditorCtrl($scope) {
var vm = $scope;
vm.options = {
toolbar: [
['style', ['bold', 'italic', 'underline', 'clear']],
]
};
}})();
And I have no idea how to use unit test to this app because my test cant find controller.
This is my controller and test :
(function () {
'use strict';
angular
.module('app')
.controller('DashboardCtrl', dashboard);
describe('test dashboard', function () {
beforeEach(module('DashboardCtrl'));
var $controller;
beforeEach(inject(function (_$controller_) {
$controller = _$controller_;
}));
describe('sum', function () {
it('1 + 1 should equal 2', function () {
var $scope = {};
var controller = $controller('DashboardCtrl', {$scope: $scope});
$scope.x = 1;
$scope.y = 2;
$scope.sum();
expect($scope.z).toBe(3);
});
it('z should default to zero', function () {
var $scope = {};
var controller = $controller('DashboardCtrl', {$scope: $scope});
expect($scope.z).toBe(0);
});
});
});
function dashboard($scope) {
$scope.name = 'Dashboard';
$scope.z = 0;
$scope.sum = function () {
$scope.z = $scope.x + $scope.y;
};
}
})();
And in karma test show me this error :
Error: [$injector:modulerr] Failed to instantiate module DashboardCtrl due to:
Error: [$injector:nomod] Module 'DashboardCtrl' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
That indicate that cant find "DashboardCtrl" controller.
the problem solved by add all modules in files options of karma.conf.js
the point is event lake of one of modules that are injected is main module that here is app stop your test, so addition to include your controller you need to add all module are mentioned.
When unit testing use $controller for getting a handle on a controller instance https://docs.angularjs.org/api/ng/service/$controller like var ctrlToTest = $controller('EditorCtrl')(newTestScope); To create a new test scope you can use var newTestScope = $rootScope.$new()
I'm trying to mock calls to localStorage.getItem, but getting error "Function expected". Here is my test code:
describe("AuthService Tests", function() {
var authSvc, httpBackend, scope;
var fakeItem = "{\"access_token\":\"giant_access_token\",\"token_type\":\"bearer\",\"expires_in\":1209599,\".issued\":\"Thu, 11 Feb 2016 13:22:45 GMT\",\".expires\":\"Thu, 25 Feb 2016 13:22:45 GMT\",\"accountType\":\"Administrator\",\"certifiedForAccess\":true}";
var returnFakeToken = function(key) { return fakeItem; }
beforeEach(module('app'));
beforeEach(inject(function (_AuthService_, $q, $rootScope, $httpBackend, $state, _config_, _messages_) {
scope = $rootScope.$new();
spyOn(localStorage, 'getItem').and.callFake(returnFakeToken);
authSvc = _AuthService_;
httpBackend = $httpBackend;
}));
describe('Test suite 1', function () {
it('should return true if user is Administrator', function () {
var result = authSvc.isAdministrator(); // error here
expect(result).toBe(true);
});
});
});
Service under test:
(function () {
'use strict';
angular
.module('app.services')
.factory('AuthService', AuthService);
AuthService.$inject = ['$q', '$rootScope', '$http', '$state', 'config', 'messages'];
function AuthService($q, $rootScope, $http, $state, config, messages) {
var userIdKey = 'userId';
var tokenKey = 'tokenKey';
var userAuthKey = 'userAuth';
var svc = {
isAdministrator: isAdministrator
};
return svc;
function isAdministrator() {
var item = localStorage.getItem(tokenKey); // eror here
var jsonparse = JSON.parse(item);
return jsonparse.accountType == 'Administrator';
}
})();
When I run tests, my mocked function returnFakeToken hadn't even been called and I got error (lines of code where error occured are marked with comments):
TypeError: Function expected
What am I doing wrong?
Thanks!
You don't need to spy on localStorage. It is a function that is built into the browser as part of the HTML5 standard. What you should be testing is that your service returns the expected value. Also, you may need to mock out localStorage if you are testing in PhantomJS.
I'm having trouble getting my tests to run due to dependencies not beeing injected correctly.
The error I'm getting is defined in the title. I've included the actual test code, the app.js & index.html file from my solution.
The problem lies with the deferred bootstrap which I'm not fimiliar with as it was included by one of my colleagues. If I remove the "app.config(function (STARTUP_CONFIG..." from the app.js file then the test runs fine
How can I correctly inject the STARTUP_CONFIG in my test?
test code
..
..
describe("test description...", function () {
var app;
var mockupDataFactory;
beforeEach(module('Konstrukt'));
beforeEach(inject(function (STARTUP_CONFIG,BUDGETS,APPLICATIONS) {
//failed attempt to inject STARTUP_CONFIG
}));
beforeEach(function () {
app = angular.module("Konstrukt");
});
beforeEach(function () {
mockupDataFactory = konstruktMockupData.getInstance();
});
it('should be accessible in app module', function () {
expect(app.pivotTableService).toNotBe(null); //this test runs fine
});
it('test decr...', inject(function ( pivotTableService) {
... //Fails here
..
..
app.js
..
..
angular.module('Konstrukt', ['ngGrid', 'ngSanitize', 'ngRoute','pasvaz.bindonce', 'ngAnimate', 'nvd3ChartDirectives', 'ui.select', 'ngProgress', 'ui.grid', 'ui.grid.edit','ui.grid.selection', 'ui.grid.cellNav', 'ui.grid.pinning', 'ui.grid.resizeColumns']);
var app = angular.module('Konstrukt');
app.config(function (STARTUP_CONFIG, BUDGETS, APPLICATIONS) {
var STARTUP_CONFIG = STARTUP_CONFIG;
var BUDGETS = BUDGETS;
var APPLICATIONS = APPLICATIONS;
});
..
..
index.html
..
..
<script>
setTimeout(function(){
window.deferredBootstrapper.bootstrap({
element: window.document.body,
module: 'Konstrukt',
resolve: {
STARTUP_CONFIG: ['$http', function ($http) {
return $http.get('/scripts/_JSON/activeBudgets.JSON');
}],
BUDGETS: ['$http', function ($http) {
return $http.get('/scripts/_JSON/activeBudgets.JSON');
}],
APPLICATIONS: ['$http', function ($http) {
return $http.get('/scripts/_JSON/applications.JSON');
}]
}
})
} , 1500);
</script>
The deferredBootstrapper will not run in your unit tests, which means the constants it normally adds to your module won't be available.
You can add a global beforeEach that provides mocked versions of them:
beforeEach(function () {
module(function ($provide) {
$provide.constant('STARTUP_CONFIG', { something: 'something' });
$provide.constant('BUDGETS', { something: 'something' });
$provide.constant('APPLICATIONS', { something: 'something' });
});
});
I want to test an Angular controller for my application fooApp, defined as follow:
var fooApp = angular.module('fooApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ]);
...
The controller, MainCtrl is defined:
"use strict";
fooApp.controller('MainCtrl', function ($scope, $rootScope, fooService) {
...
}
So I've tested several ways to create a test, like this one:
'use strict';
describe('MainController test', function () {
var scope;
var controller;
beforeEach(function () {
angular.mock.module('ngRoute', []);
angular.mock.module('ngAnimate', []);
angular.mock.module('hmTouchEvents', []);
angular.module('cwfApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ]);
angular.mock.inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('MainCtrl', {
$scope: scope
});
});
});
it('should display a list', function () {
console.log('-------------- Run Test 1 | ' + scope);
expect(scope.currentStep).toBe(1);
});
});
and the result:
Error: [$injector:modulerr] http://errors.angularjs.org/1.2.16-build.64+sha.245de33/$injector/modulerr?p0=undefined&p1=Error%3A%20%5Bng%3Aareq%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.2.16-build.64%2Bsha.245de33%2Fng%2Fareq%3Fp0%3Dfn%26p1%3Dnot%2520a%2520function%252C%2520got%2520undefined%0A%20%20%20%20at%20Error%20(%3Canonymous%3E)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A6%3A471%0A%20%20%20%20at%20wb%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A18%3A360)%0A%20%20%20%20at%20Qa%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A18%3A447)%0A%20%20%20%20at%20nc%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A31%3A191)%0A%20%20%20%20at%20Object.d%20%5Bas%20invoke%5D%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A33%3A176)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A32%3A254%0A%20%20%20%20at%20Array.forEach%20(native)%0A%20%20%20%20at%20r%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A7%3A298)%0A%20%20%20%20at%20e%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A32%3A9)
at Error (<anonymous>)
at d:/dev/foo/app/bower_components/angular/angular.min.js:6:471
at d:/dev/foo/app/bower_components/angular/angular.min.js:32:400
at Array.forEach (native)
at r (d:/dev/foo/app/bower_components/angular/angular.min.js:7:298)
at e (d:/dev/foo/app/bower_components/angular/angular.min.js:32:9)
at Object.$b [as injector] (d:/dev/foo/app/bower_components/angular/angular.min.js:35:98)
at workFn (d:/dev/foo/app/bower_components/angular-mocks/angular-mocks.js:2142:52)
at Object.window.inject.angular.mock.inject [as inject] (d:/dev/foo/app/bower_components/angular-mocks/angular-mocks.js:2133:37)
at null.<anonymous> (d:/dev/foo/test/jasmine/todo.test.js:15:22)
TypeError: Cannot read property 'currentStep' of undefined
at null.<anonymous> (d:/dev/foo/test/jasmine/todo.test.js:25:21)
Chrome 31.0.1650 (Windows 7): Executed 1 of 1 (1 FAILED) ERROR (0.023 secs / 0.015 secs)
I've also tested with beforeEach(angular.mock.module('cwfApp')); (instead of the first beforeEach in the previous code), but the error is almost the same.
Regarding my karma.conf.js file, I set this list of files:
files: [
'app/bower_components/angular/angular.min.js',
'app/bower_components/angular-route/angular-route.min.js',
'app/bower_components/hammerjs/hammer.min.js',
'app/bower_components/angular-hammer/angular-hammer.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/js/foo-application.js',
'app/js/foo-controllers.js',
'app/js/foo-services.js',
'app/js/foo-router.js',
'test/jasmine/*.js'
],
The injection seems to fail, but I don't really understand what is missing or wrong in my configuration. The stacktrace above does not give a lot of explanations...
Any idea?
I'm using Angular 1.2.8.
Regards
Edit, with the code provided by #Engineer:
beforeEach(angular.mock.module('fooApp'));
beforeEach(angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('MainCtrl', {
$scope: scope
});
}));
it('should display a list', function () {
console.log('-------------- Run Test 1 | ' + scope);
expect(scope.currentStep).toBe(1);
});
The error is almost the same:
Error: [$injector:modulerr] http://errors.angularjs.org/1.2.16-build.64+sha.245de33/$injector/modulerr?p0=cwfApp&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.2.16-build.64%2Bsha.245de33%2F%24injector%2Fmodulerr%3Fp0%3DngAnimate%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.2.16-build.64%252Bsha.245de33%252F%2524injector%252Fnomod%253Fp0%253DngAnimate%250A%2520%2520%2520%2520at%2520Error%2520(%253Canonymous%253E)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A6%253A471%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A20%253A260%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A21%253A262%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A32%253A69%250A%2520%2520%2520%2520at%2520Array.forEach%2520(native)%250A%2520%2520%2520%2520at%2520r%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A7%253A298)%250A%2520%2520%2520%2520at%2520e%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A32%253A9)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F5961971009303638e9ad386869316e8c83f67e56%253A32%253A86%250A%2520%2520%2520%2520at%2520Array.forEach%2520(native)%0A%20%20%20%20at%20Error%20(%3Canonymous%3E)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A6%3A471%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A32%3A400%0A%20%20%20%20at%20Array.forEach%20(native)%0A%20%20%20%20at%20r%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A7%3A298)%0A%20%20%20%20at%20e%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A32%3A9)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A32%3A86%0A%20%20%20%20at%20Array.forEach%20(native)%0A%20%20%20%20at%20r%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A7%3A298)%0A%20%20%20%20at%20e%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F5961971009303638e9ad386869316e8c83f67e56%3A32%3A9)
at Error (<anonymous>)
at d:/dev/foo/app/bower_components/angular/angular.min.js:6:471
at d:/dev/foo/app/bower_components/angular/angular.min.js:32:400
at Array.forEach (native)
at r (d:/dev/foo/app/bower_components/angular/angular.min.js:7:298)
at e (d:/dev/foo/app/bower_components/angular/angular.min.js:32:9)
at Object.$b [as injector] (d:/dev/foo/app/bower_components/angular/angular.min.js:35:98)
at workFn (d:/dev/foo/app/bower_components/angular-mocks/angular-mocks.js:2142:52)
TypeError: Cannot read property 'currentStep' of undefined
at null.<anonymous> (d:/dev/foo/test/jasmine/todo.test.js:20:21)
I will try to create a fiddle to reproduce my problem...
My problem was in fact due to a little mistake in karma.conf.js. Indeed, my application is defined as follow:
var fooApp = angular.module('fooApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ]);
and my karma.conf.js loads the following scripts:
files: [
'app/bower_components/angular/angular.min.js',
'app/bower_components/angular-route/angular-route.min.js',
'app/bower_components/hammerjs/hammer.min.js',
'app/bower_components/angular-hammer/angular-hammer.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/js/foo-application.js',
'app/js/foo-controllers.js',
'app/js/foo-services.js',
'app/js/foo-router.js',
'test/jasmine/*.js'
], ...
but the module ngAnimate was not loaded. So I just added that line:
'app/bower_components/angular-animate/angular-animate.min.js',
and it works!
You are redefining fooApp module in the test.
You need to load it with Angular in the test code like this:
angular.mock.module('ngRoute', 'ngAnimate', 'hmTouchEvents', 'fooApp');
Try like this:
describe('MainController test', function () {
var scope;
var controller;
beforeEach(angular.mock.module('fooApp'));
beforeEach(angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('MainCtrl', {
$scope: scope
});
});
it('should display a list', function () {
console.log('-------------- Run Test 1 | ' + scope);
expect(scope.currentStep).toBe(1);
});
});
The thing, you need to understand, is if you declare some module with its dependencies(like angular.module('fooApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ])), when you inject the module(fooApp) on your tests, you don't need to inject the dependency modules either.