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.
Related
I've been beating my head against the wall trying to figure out why these tests aren't working using Karma and Jasmine to test an Ionic Controller. I'm new to ionic, karma unit testing, angularjs, and just about everything else here. Can anyone identify the reason these tests are failing?
Here's my controller:
angular.module('starter.controllers')
.controller('TemplateCtrl', function($scope) {
$scope.text = 'Hello Template';
});
Here's my test:
describe('Template Controller', function(){
var scope;
beforeEach(module('starter.controllers'));
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
$controller('TemplateCtrl', {$scope: scope});
}));
// tests start here
it('should have scope defined', function() {
expect(scope).toBeDefined();
});
it('should have text set to Hello Template', function(){
expect(scope.text).toEqual('Hello Template');
});
});
Test results:
PhantomJS 2.1.1 (Linux 0.0.0) Template Controller should have scope defined FAILED
Expected undefined to be defined.
PhantomJS 2.1.1 (Linux 0.0.0) Template Controller should have text set to Hello Template FAILED
TypeError: undefined is not an object (evaluating 'scope.text') in controller-tests/template.tests.js (line 23)
Thank you for your time.
Phil Identified the problem, without me even posting my karma config file:
Does Karma include the files that define your starter.controllers
module as well as the TemplateCtrl controller?
Simply adding the path to the karma config file fixed my issue.
Thank you Phil!
I'm not sure why, but when I run a new Karma test for my AngularJS app I get output from a previous version of my test file. I'm new to Karma testing so I'm sure I'm missing something somewhere.
PhantomJS 2.1.1 (Windows 7 0.0.0) Calendar Constructor instantiates a calendar with a year, month, and options FAILED
(This is what I had previously set as output for the this test)
I have no idea how I'm getting the above output when my test file looks like this.
test.js
'use strict';
describe('holidays', function () {
var scope, controller;
beforeEach(function(){
module('holiday');
});
describe('HolidaysController', function(){
beforeEach(inject(function($rootScope, $controller){
scope = $rootScope;
controller = $controller('HolidaysController', {
'vm': scope
});
}));
it('should work', function(){
expect(vm.tooltips).toBe(false);
});
});
});
Angular version: 1.4.0
Karma version: 0.13.22
PhantomJS version:
2.1.1
I figured it out, I had another terminal open that I had previously ran karma in. After closing both terminals and reopening one the problem was fixed.
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'
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)
}
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?