Argument 'ListCtrl' is not a function, got undefined - angularjs

I'm new to Karma and Jasmine, and wanted to get some assistance trying to use grunt test on controllers. The error I get when I run the test is (shortened):
Running "karma:unit" (karma) task
INFO [karma]: Karma v0.10.9 server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
WARN [watcher]: Pattern "/Users/doronkatz/Development/Angular/Chapter4/test/mock/**/*.js" does not match any file.
INFO [Chrome 31.0.1650 (Mac OS X 10.9.1)]: Connected on socket 34BLydt6tI-UpBXZQBCZ
Chrome 31.0.1650 (Mac OS X 10.9.1) Controllers ListCtrl should have list of recipes FAILED
Error: [ng:areq] Argument 'ListCtrl' is not a function, got undefined
http://errors.angularjs.org/1.2.6/ng/areq?p0=ListCtrl&p1=not%20a%20function%2C%20got%20undefined
at /Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular/angular.js:78:12
at assertArg (/Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular/angular.js:1360:11)
at assertArgFn (/Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular/angular.js:1370:3)
at /Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular/angular.js:6755:9
at null.<anonymous> (/Users/doronkatz/Development/Angular/Chapter4/test/spec/controllers/controllers.js:21:14)
at Object.invoke (/Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular/angular.js:3697:17)
at workFn (/Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular-mocks/angular-mocks.js:2102:20)
Error: Declaration Location
at window.inject.angular.mock.inject (/Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular-mocks/angular-mocks.js:2087:25)
at null.<anonymous> (/Users/doronkatz/Development/Angular/Chapter4/test/spec/controllers/controllers.js:17:16)
at null.<anonymous> (/Users/doronkatz/Development/Angular/Chapter4/test/spec/controllers/controllers.js:13:3)
at /Users/doronkatz/Development/Angular/Chapter4/test/spec/controllers/controllers.js:1:1
Expected undefined to equal [ 1, 2, 3 ].
Error: Expected undefined to equal [ 1, 2, 3 ].
at null.<anonymous> (/Users/doronkatz/Development/Angular/Chapter4/test/spec/controllers/controllers.js:28:30)
Chrome 31.0.1650 (Mac OS X 10.9.1) Controllers EditController should save the recipe FAILED
...
Controllers.js test:
describe('Controllers', function() {
var $scope, ctrl;
//you need to indicate your module in a test
beforeEach(module('chapter4App',
['directives', 'services']));
beforeEach(function() {
this.addMatchers({
toEqualData: function(expected) {
return angular.equals(this.actual, expected);
}
});
});
describe('ListCtrl', function() {
var mockBackend, recipe;
// The _$httpBackend_ is the same as $httpBackend. Only written this way to
// differentiate between injected variables and local variables
beforeEach(inject(function($rootScope, $controller, _$httpBackend_, Recipe) {
recipe = Recipe;
mockBackend = _$httpBackend_;
$scope = $rootScope.$new();
ctrl = $controller('ListCtrl', {
$scope: $scope,
recipes: [1, 2, 3]
});
}));
it('should have list of recipes', function() {
expect($scope.recipes).toEqual([1, 2, 3]);
});
});
Please look at https://github.com/doronkatz/angularjs-book/tree/master/chapter4 which has the code, including test script for the controller, and controller.

4th line:
WARN [watcher]: Pattern "/Users/doronkatz/Development/Angular/Chapter4/test/mock/*/.js" does not match any file.
Sooo, maybe You dont load all required files?

Related

Angular Jasmine tests error - $injector is not defined

Using Karma Im back filling tests on a project (someone else wrote the angular app - I have the 'pleasure')...
Project written in coffeescript.
I have the following jasmine test:
'use strict'
describe 'module:file:controller', () ->
$controller = null
$scope = null
$state = null
$stateParams = null
Page = null
File = null
beforeEach (module 'forge')
beforeEach inject(($rootScope, _$controller_, _$state_, _$stateParams_, _Page_, _File_) ->
$scope = $rootScope.$new()
$state = _$state_
$stateParams = _$stateParams_
Page = _Page_
File = _File_
$controller = _$controller_
)
describe 'init', () ->
controller = null
beforeEach () ->
$state = {}
$stateParams = {}
controller = $controller('fileController', {$scope: $scope, $state: $state, $stateParams: $stateParams, Page: Page, File: File})
it 'should set page title to File', () ->
spyOn(Page, 'title')
expect(Page.title).toHaveBeenCalledWith 'Files'
it 'should do something with delete', () ->
spyOn(Page, 'addAlert')
$scope.delete(1)
expect(Page.addAlert).toHaveBeenCalled()
Here is the controller:
forge.controller 'fileController', ($scope, $state, $stateParams, Page, File) ->
# Inject base controller
$injector.invoke BaseController, this, { $scope: $scope, Factory: File }
# Set page title
Page.title 'Files'
console.log $stateParams
console.log $state
# Delete
$scope.delete = (id) ->
Page.addAlert 'success', '[TEST] Deleting file...'
console.log $stateParams
console.log $state
# Delegate actions
switch $stateParams.action
when 'delete' then $scope.delete $stateParams.id
I have a bunch of unit test running fine. The application is built using grunt and all code is compiled into /dist/assets/js/app.js.
My karma config:
module.exports = (config) ->
config.set
files: [
'../../dist/assets/js/app.js'
'../../bower_components/angular-mocks/angular-mocks.js'
'unit/**/**.test.coffee'
'../module/**/test/unit/**/*.unit.coffee'
]
preprocessors:
'unit/**/**.test.coffee': ['coffee']
'../module/**/test/unit/**/*.unit.coffee': ['coffee']
frameworks: ['jasmine']
reporters: ['progress']
browsers: ['Chrome', 'Firefox', 'Safari']
logLevel: 'ERROR'
jasmineNodeOpts:
onComplete: null
isVerbose: false
showColors: true
includeStackTrace: false
defaultTimeoutInterval: 30000
When I run the tests I receive:
Chrome 48.0.2564 (Mac OS X 10.11.3) module:file:controller init should set page title to File FAILED
ReferenceError: $injector is not defined
at new <anonymous> (/Users/usr1/repos/proj/dist/assets/js/forge.js:75397:3)
at Object.instantiate (/Users/usr1/repos/proj/dist/assets/js/forge.js:14449:14)
at /Users/usr1/repos/proj/dist/assets/js/forge.js:19700:28
at /Users/usr1/repos/proj/bower_components/angular-mocks/angular-mocks.js:2170:12
at Object.<anonymous> (/Users/usr1/repos/proj/src/module/file/test/unit/controller/file-controller.unit.js:25:27)
Expected spy title to have been called with [ 'Files' ] but it was never called.
at Object.<anonymous> (/Users/usr1/repos/proj/src/module/file/test/unit/controller/file-controller.unit.js:35:33)
Chrome 48.0.2564 (Mac OS X 10.11.3) module:file:controller init should do something with delete FAILED
ReferenceError: $injector is not defined
at new <anonymous> (/Users/usr1/repos/proj/dist/assets/js/forge.js:75397:3)
at Object.instantiate (/Users/usr1/repos/proj/dist/assets/js/forge.js:14449:14)
at /Users/usr1/repos/proj/dist/assets/js/forge.js:19700:28
at /Users/usr1/repos/proj/bower_components/angular-mocks/angular-mocks.js:2170:12
at Object.<anonymous> (/Users/usr1/repos/proj/src/module/file/test/unit/controller/file-controller.unit.js:25:27)
TypeError: $scope.delete is not a function
at Object.<anonymous> (/Users/usr1/repos/proj/src/module/file/test/unit/controller/file-controller.unit.js:39:23)
Chrome 48.0.2564 (Mac OS X 10.11.3): Executed 63 of 63 (2 FAILED) (1.155 secs / 1.134 secs)
Checked all the paths and that the controllers are presentin teh built app.js file.
At a loss now as to why this message persists and any pointers would be great.
My application was minified - this would prevent this working.
angular injector docs.

Angular Jasmine/Karma basic Test Spec

So this is an odd one, I have setup a seed app and I have created a basic test script on a controller and it works, but then I move to a second one and it gives me the undefined message I am seeing all over the internet , here is the code for both , since I am new to karma-jasmine someone might see what I am missing in my test
First test, (This one works)
//Controller
'use strict';
angular.module('myApp',[])// jshint ignore:line
.controller('HelloWorldController', ['$scope', function($scope) {
$scope.greeting = 'Hello World!';
}]);
//Test Script ***********************************************************
describe('Hello World example ', function() {
beforeEach(module('myApp'));
var HelloWorldController,
scope;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
HelloWorldController = $controller('HelloWorldController', {
$scope: scope
});
}));
it('says hello world!', function () {
expect(scope.greeting).toEqual("Hello World!");
});
});
This one however does not work
// About Controller
'use strict';
angular.module('myApp.module.controller.about', ['ngRoute'])// jshint ignore:line
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/about', {
controller: 'aboutController',
templateUrl: 'com/modules/about/views/about.html',
hideMenus: true,
protectedArea: false,
title: 'About',
description: '',
keywords: ''
});
}])
.controller('aboutController', ['$scope', function($scope) {
$scope.greeting = 'This is the about message!';
}]);
//Test Script *****************************************
describe('AboutController Test', function() {
beforeEach(module('myApp.module.controller.about'));
var aboutController,
scope;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
aboutController = $controller('aboutController', {
$scope: scope
});
}));
it('about greeting says "This is the about message!"', function () {
expect(scope.greeting).toEqual("This is the about message!");
});
});
Here is what I am getting from Karma
[12:58:47] Starting 'karma'...
INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket I1XiLeRfz1SiV-wY2Y5x with id 89911408
PhantomJS 1.9.8 (Mac OS X 0.0.0) AboutController Test about greeting says "This is the about message!" FAILED
Error: [$injector:modulerr] http://errors.angularjs.org/1.4.3/$injector/modulerr?p0=myApp.module.controller.about&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.3%2F%24injector%2Fmodulerr%3Fp0%3DngRoute%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.4.3%252F%2524injector%252Fnomod%253Fp0%253DngRoute%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A25%250A%2520%2520%2520%2520at%2520a%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A24)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A26%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A38%250A%2520%2520%2520%2520at%2520m%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A8)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A39)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A38%250A%2520%2520%2520%2520at%2520m%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A8)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A39)%250A%2520%2520%2520%2520at%2520eb%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular.min.js%253F54ede95d02e505e15087f0b61af505f27ee25156%253A41)%250A%2520%2520%2520%2520at%2520workFn%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252Fwebdev%252FE21%252Fsrc%252Flib%252Fjs%252Fangular%252Fangular-mocks.js%253F7822c97bd61398189431d2e3db4e01ed8f9d4f10%253A2391)%250A%2520%2520%2520%2520at%2520attemptSync%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1789)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1777%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1762%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A627%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A357%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A2360%250A%2520%2520%2520%2520at%2520attemptAsync%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1819)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1774%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1762%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A627%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A2354%250A%2520%2520%2520%2520at%2520attemptAsync%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1819)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1774%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A1762%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A627%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A2215%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fjasmine-core%252Flib%252Fjasmine-core%252Fjasmine.js%253F578a1e5ff14db21b04e2d6db7fd0eda37042440c%253A678%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fnode_modules%252Fkarma-jasmine%252Flib%252Fadapter.js%253F3030709c83121e1b2ca4d1e657306b834fc13350%253A318%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fkarma.js%253A182%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fcontext.html%253A75%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A39%0A%20%20%20%20at%20m%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A8)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A39)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A38%0A%20%20%20%20at%20m%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A8)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A39)%0A%20%20%20%20at%20eb%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular.min.js%3F54ede95d02e505e15087f0b61af505f27ee25156%3A41)%0A%20%20%20%20at%20workFn%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2Fwebdev%2FE21%2Fsrc%2Flib%2Fjs%2Fangular%2Fangular-mocks.js%3F7822c97bd61398189431d2e3db4e01ed8f9d4f10%3A2391)%0A%20%20%20%20at%20attemptSync%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1789)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1777%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1762%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A627%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A357%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A2360%0A%20%20%20%20at%20attemptAsync%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1819)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1774%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1762%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A627%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A2354%0A%20%20%20%20at%20attemptAsync%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1819)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1774%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A1762%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A627%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A2215%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F578a1e5ff14db21b04e2d6db7fd0eda37042440c%3A678%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fadapter.js%3F3030709c83121e1b2ca4d1e657306b834fc13350%3A318%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fkarma.js%3A182%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fcontext.html%3A75
at /webdev/E21/src/lib/js/angular/angular.min.js:39
at m (/webdev/E21/src/lib/js/angular/angular.min.js:8)
at g (/webdev/E21/src/lib/js/angular/angular.min.js:39)
at eb (/webdev/E21/src/lib/js/angular/angular.min.js:41)
at workFn (/webdev/E21/src/lib/js/angular/angular-mocks.js:2391)
TypeError: 'undefined' is not an object (evaluating 'scope.greeting')
at /webdev/E21/src/com/modules/about/aboutController_spec.js:17
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 1 of 2 (1 FAILED) (0 secs / 0.007 secPhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 2 of 2 (1 FAILED) (0 secs / 0.01 secsPhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 2 of 2 (1 FAILED) (0.004 secs / 0.01 secs)
Someone who has more familiarity with this might see what I am missing, since I know the first test works I know my setup is correct so the issue must lie in the actual test spec for the about controller, any help would be appreciated .
After playing around with it , I found this to be a bit deceptive the issue wasn't in the specs at all, the specs worked, the issue was in the karma conf file, I had included the angular-mocks but nothing else once I expanded my bower_components from just mocks to a few others I was able to get all the test to run and complete without issue.
Here is what I included for those having a similar problem , (This is under the files area on the karma.conf file)
'bower_components/angular/angular.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'../src/com/**/*.js',
'../src/com/**/*_spec.js'

Jasmine Angular http testing

I'm trying to test a REST API wrapped in an AngularJS service using Jasmine async testing. I know, I should be, and am, running tests on the API on the server, and using a mock $httpBackend to test the service.
but when i test this code :
'use strict';
describe('controllers: SiteCtrl', function() {
var scope, httpBackend, http, controller;
var zipcode = "94305";
beforeEach(module('ngMockE2E'));
beforeEach(module('ironridge'));
beforeEach(inject(function($rootScope, $controller, $httpBackend, $http) {
scope = $rootScope.$new();
httpBackend = $httpBackend;
controller = $controller;
http = $http;
// httpBackend.w hen('http://api.zippopotam.us/us/' + zipcode).respond({});
$controller('SiteCtrl', {
$scope: scope,
$http: $http
});
}));
it('should define more than 5 awesome things',function (){
expect(scope.nav.radio).toMatch('site');
});
it("should match a correct zip code", function() {
httpBackend.expectGET('http://api.zippopotam.us/us/' + zipcode).responde(200);
http.get('http://api.zippopotam.us/us/' + zipcode).
success(function(data) {
console.log(data);
}).
error(function(status) {
console.log(status);
});
httpBackend.flush();
});
});
but i get an error when running test
[16:01:41] all files 33.1 kB
[16:01:41] Finished 'scripts' after 1.06 s
[16:01:41] Starting 'test'...
WARN [watcher]: Pattern "/home/hpro/ironridge/src/**/*.mock.js" does not match any file.
INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Linux 0.0.0)]: Connected on socket Cfi_cbdTVW-YOCX8_BaV with id 45541989
PhantomJS 1.9.8 (Linux 0.0.0) controllers: SiteCtrl should match a correct zip code FAILED
TypeError: 'undefined' is not a function (evaluating 'httpBackend.expectGET('http://api.zippopotam.us/us/' + zipcode).responde(200)')
at /home/hpro/ironridge/src/app/site/site.controller.spec.js:28
PhantomJS 1.9.8 (Linux 0.0.0): Executed 2 of 2 (1 FAILED) (0.006 secs / 0.059 secs)
[16:01:42] 'test' errored after 1.41 s
[16:01:42] Error: 1
at formatError (/usr/lib/node_modules/gulp/bin/gulp.js:169:10)
at Gulp.<anonymous> (/usr/lib/node_modules/gulp/bin/gulp.js:195:15)
at Gulp.emit (events.js:107:17)
at Gulp.Orchestrator._emitTaskDone (/home/hpro/ironridge/node_modules/gulp/node_modules/orchestrator/index.js:264:8)
at /home/hpro/ironridge/node_modules/gulp/node_modules/orchestrator/index.js:275:23
at finish (/home/hpro/ironridge/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:21:8)
at cb (/home/hpro/ironridge/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:29:3)
at removeAllListeners (/home/hpro/ironridge/node_modules/karma/lib/server.js:215:7)
at Server.<anonymous> (/home/hpro/ironridge/node_modules/karma/lib/server.js:226:9)
at Server.g (events.js:199:16)
Thanks for help :D
You cannot do E2E tests with httpBackend.
First option, to forfit E2E tests, and make tests for $httpBackend, without e2e tests and run it from grunt running grunt test. Which would be unit tests.
Second option, which you can mix with first is to create separate file for E2E tests, also in Jasmine syntax and run it independly with protractor.
In edited code, use flush before calling handle.success.
Flushing means that get operation goes into action.
Instead of:
$http.get('http://api.zippopotam.us/us/' + zipcode).
then(handler.success,handler.error);
httpBackend.flush();
Write:
var prom = $http.get('http://api.zippopotam.us/us/' + zipcode);
httpBackend.flush();
prom.then(handler.success,handler.error);
It's possible that handler.success is called before flush is made.

Angular $watchGroup and $watch undefined in Jasmine tests

Whoever said that testing Angular apps is a breeze had to be joking. Since I started writing tests for our Angular application, I consider it a great success when I move from one error message to another when running karma. Most of the examples online seem to be simplified and are not really transferable to my error cases. Now, onto the current problem I have:
I have angular-mocks.js and other angular dependencies hooked up in karma.conf.js file, I have tested config block of our app (controllers and templates matching routes) and the tests are green. Now I am trying to test controller which has $watchGroup - for some bloody reason $watchGroup is undefined (and also $watch when I tried to use it) in my jasmine test. When I comment the $watchGroup out my dummy test expect(true).toBe(true) is green, but with $watchGroup code in the controller (which is working fine btw) karma console reports that $watchGroup is undefined.
This is the code in the controller:
$scope.$watchGroup([
'Message.AgeRangeMin',
'Message.AgeRangeMax',
'Message.SubscriberListFileId',
'Message.SmsSettings.SelectedSender',
'Message.EmailSettings.SelectedTemplate',
'Message.PushSettings.SelectedSenders.length',
'Message.SocialSettings.SelectedSocialNetworks.length'
], $scope.triggerUserForecast
);
$scope.triggerUserForecast = function () {
commsMgmtHttpService.GetTotalReach($scope.Message)
.then(function (data) {
$scope.UserDeliveryForecast = data;
}, function () {
$scope.UserDeliveryForecast.TotalUserReach = 0;
});
};
This is my test case:
describe('forge.communications.CommsApp', function () {
beforeEach(module('forge.communications.CommsApp'));
describe('CreateScheduledMessageController', function () {
var ctrl, $scope, $rootScope, $controller, $httpBackend;
beforeEach(function () {
inject(function (_$rootScope_, _$controller_, _$httpBackend_) {
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
$controller = _$controller_('CreateScheduledMessageController', {
$scope: $scope,
$scope: {
ModelState: new ModelState($scope)
},
$location: $location,
$modal: $modal,
$upload: $upload
});
})
});
it("dummy should be true", function () {
expect(true).toBe(true);
});
});
});
This is the Karma console error I am getting:
Chrome 40.0.2214 (Windows 7) forge.communications.CommsApp CreateScheduledMessageController dummy should be defined FAILED TypeError: undefined is not a function at new (C:/work/theforge/src/TheForge/dist/CommsApp.js:2581:12) at invoke (C:/work/theforge/src/TheForge/Scripts/angular.js:4118:17) at Object.instantiate (C:/work/theforge/src/TheForge/Scripts/angular.js:4129:23) at C:/work/theforge/src/TheForge/Scripts/angular.js:8320:28 at Object. (C:/work/theforge/src/TheForge/FrontEndTests/CommsAppTests/unit/Controllers/CreateScheduledMessage/CreateScheduledMessageController.spec.js:31:31) at Object.invoke (C:/work/theforge/src/TheForge/Scripts/angular.js:4118:17) at Object.workFn (C:/work/theforge/src/TheForge/Scripts/angular-mocks.js:2257:20) at window.inject.angular.mock.inject (C:/work/theforge/src/TheForge/Scripts/angular-mocks.js:2229:37) at Object. (C:/work/theforge/src/TheForge/FrontEndTests/CommsAppTests/unit/Controllers/CreateScheduledMessage/CreateScheduledMessageController.spec.js:22:13) Error: Declaration Location at window.inject.angular.mock.inject (C:/work/theforge/src/TheForge/Scripts/angular-mocks.js:2228:25)at Object. (C:/work/theforge/src/TheForge/FrontEndTests/CommsAppTests/unit/Controllers/CreateScheduledMessage/CreateScheduledMessageController.spec.js:22:13) Chrome 40.0.2214 (Windows 7): Executed 15 of 15 (1 FAILED) (0 secs / 0.12 secs) WARN [web-server]: 404: /forge/signalr/negotiate?clientProtocol=1.4&connectionDaChrome 40.0.2214 (Windows 7): Executed 15 of 15 (1 FAILED) (0.415 secs / 0.12 secs)
Any advice will be of much help to me.
Thanks.
It looks like I was overwriting the $controller's $scope property in jasmine test. Removing the following lines of code, following #Chandermani's advice fixed my problem.
$scope: {
ModelState: new ModelState($scope)
}

Always-true test failing in Karma - why?

I am trying to write Karma tests for an Angular app. This test is failing:
describe('Controller: AdherenceCtrl', function () {
// load the controller's module
beforeEach(module('myApp'));
var MainCtrl,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
// do nothing for the moment
// scope = $rootScope.$new();
// MainCtrl = $controller('AdherenceCtrl', {
// $scope: scope
// });
}));
it('should pass a basic test', function () {
expect([1,2].length).toBe(2);
});
});
If I delete the beforeEach(inject section, though, it passes.
describe('Controller: AdherenceCtrl', function () {
// load the controller's module
beforeEach(module('myApp'));
var MainCtrl,
scope;
it('should pass a basic test', function () {
expect([1,2].length).toBe(2);
});
});
What is wrong with the beforeEach(inject section?
The error message I get from Karma is this, which I cannot parse:
Chrome 31.0.1650 (Mac OS X 10.9.0) Controller: AdherenceCtrl should pass a basic test FAILED
Error: [$injector:modulerr] http://errors.angularjs.org/undefined/$injector/modulerr?p0=astellasRiskfactorcalcAppApp&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2Fundefined%2F%24injector%2Fmodulerr%3Fp0%3DngRoute%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252Fundefined%252F%2524injector%252Fnomod%253Fp0%253DngRoute%250A%2520%2520%2520%2520at%2520Error%2520(%253Canonymous%253E)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252FUsers%252Fanna%252FDropbox%252Fprojects%252Fastellas-riskfactorcalc-app%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F1383935561000%253A6%253A453%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252FUsers%252Fanna%252FDropbox%252Fprojects%252Fastellas-riskfactorcalc-app%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F1383935561000%253A20%253A119%250A%2520%2520%2520%2520at%2520a%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252FUsers%252Fanna%252FDropbox%252Fprojects%252Fastellas-riskfactorcalc-app%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F1383935561000%253A19%253A353)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252FUsers%252Fanna%252FDropbox%252Fprojects%252Fastellas-riskfactorcalc-app%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F1383935561000%253A20%253A14%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252FUsers%252Fanna%252FDropbox%252Fprojects%252Fastellas-riskfactorcalc-app%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F1383935561000%253A28%253A434%250A%2520%2520%2520%2520at%2520Array.forEach%2520(native)%250A%2520%2520%2520%2520at%2520q%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252FUsers%252Fanna%252FDropbox%252Fprojects%252Fastellas-riskfactorcalc-app%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F1383935561000%253A7%253A261)%250A%2520%2520%2520%2520at%2520e%2520(http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252FUsers%252Fanna%252FDropbox%252Fprojects%252Fastellas-riskfactorcalc-app%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F1383935561000%253A28%253A374)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fabsolute%252FUsers%252Fanna%252FDropbox%252Fprojects%252Fastellas-riskfactorcalc-app%252Fapp%252Fbower_components%252Fangular%252Fangular.min.js%253F1383935561000%253A28%253A451%0A%20%20%20%20at%20Error%20(%3Canonymous%3E)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2FUsers%2Fanna%2FDropbox%2Fprojects%2Fastellas-riskfactorcalc-app%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F1383935561000%3A6%3A453%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2FUsers%2Fanna%2FDropbox%2Fprojects%2Fastellas-riskfactorcalc-app%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F1383935561000%3A29%3A262%0A%20%20%20%20at%20Array.forEach%20(native)%0A%20%20%20%20at%20q%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2FUsers%2Fanna%2FDropbox%2Fprojects%2Fastellas-riskfactorcalc-app%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F1383935561000%3A7%3A261)%0A%20%20%20%20at%20e%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2FUsers%2Fanna%2FDropbox%2Fprojects%2Fastellas-riskfactorcalc-app%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F1383935561000%3A28%3A374)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2FUsers%2Fanna%2FDropbox%2Fprojects%2Fastellas-riskfactorcalc-app%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F1383935561000%3A28%3A451%0A%20%20%20%20at%20Array.forEach%20(native)%0A%20%20%20%20at%20q%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2FUsers%2Fanna%2FDropbox%2Fprojects%2Fastellas-riskfactorcalc-app%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F1383935561000%3A7%3A261)%0A%20%20%20%20at%20e%20(http%3A%2F%2Flocalhost%3A9876%2Fabsolute%2FUsers%2Fanna%2FDropbox%2Fprojects%2Fastellas-riskfactorcalc-app%2Fapp%2Fbower_components%2Fangular%2Fangular.min.js%3F1383935561000%3A28%3A374)
at Error (<anonymous>)
at /Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js:6:453
at /Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js:29:262
at Array.forEach (native)
at q (/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js:7:261)
at e (/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js:28:374)
at Object.Xb [as injector] (/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js:32:427)
at workFn (/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/test/angular-mocks.js:2114:52)
Here is the URL decoded stack trace:
Error: [$injector:modulerr] http://errors.angularjs.org/undefined/$injector/modulerr?p0=astellasRiskfactorcalcAppApp&p1=Error: [$injector:modulerr] http://errors.angularjs.org/undefined/$injector/modulerr?p0=ngRoute&p1=Error: [$injector:nomod] http://errors.angularjs.org/undefined/$injector/nomod?p0=ngRoute
at Error (<anonymous>)
at http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:6:453
at http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:20:119
at a (http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:19:353)
at http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:20:14
at http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:28:434
at Array.forEach (native)
at q (http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:7:261)
at e (http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:28:374)
at http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:28:451
at Error (<anonymous>)
at http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:6:453
at http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:29:262
at Array.forEach (native)
at q (http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:7:261)
at e (http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:28:374)
at http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:28:451
at Array.forEach (native)
at q (http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:7:261)
at e (http://localhost:9876/absolute/Users/anna/Dropbox/projects/astellas-riskfactorcalc-app/app/bower_components/angular/angular.min.js?1383935561000:28:374)
It mentions ngRoute in line 1. The documentation says:
In AngularJS 1.2.0 and later, ngRoute has been moved to its own module. If you are getting this error after upgrading to 1.2.x, be sure that you've installed ngRoute.
If you are using Angular 1.2.x, have you tried adding ngRoute as a dependency of myApp?

Resources