Karma error: myApp is not defined - angularjs

I'm trying to test the Angular seed project with a clean install of Karma in a different directory. I haven't modified the Angular seed project. Why are myApp and myApp.View1Ctrl not defined?
My tests are:
describe("Unit Testing Examples", function () {
//beforeEach(angular.mock.module('myApp'));
beforeEach(module('myApp'));
it('should have a View1Ctrl controller', function () {
expect(myApp).toBeDefined();
expect(myApp.View1Ctrl).toBeDefined();
});
});
The output is:
INFO [watcher]: Changed file "C:/Projects/KarmaJasmine/KarmaJasmine/test/placeho
lder.js".
Chrome 37.0.2062 (Windows 8.1) Unit Testing Examples should have a View1Ctrl controller FAILED
ReferenceError: myApp is not defined
at null.<anonymous> (C:/Projects/KarmaJasmine/KarmaJasmine/test/plac
eholder.js:8:16)
Chrome 37.0.2062 (Windows 8.1): Executed 6 of 6 (1 FAILED) (0 secs / 0.054 secs)
Chrome 37.0.2062 (Windows 8.1): Executed 6 of 6 (1 FAILED) (0.061 secs / 0.054 s
ecs)
My karma.conf has the following section, and I confirmed that the files exist:
files: [
'../AngularSeed/app/bower_components/angular/angular.js',
'../AngularSeed/app/bower_components/angular-route/angular-route.js',
'../AngularSeed/app/bower_components/angular-mocks/angular-mocks.js',
'../AngularSeed/app/app.js',
'../AngularSeed/app/components/**/*.js',
'../AngularSeed/app/view*/**/*.js'
, 'test/placeholder.js'
],
Please let me know if I need to post more details. I tried to read through all of the similar questions, and haven't found an answer.

You have myApp variable that is not defined in your spec. Define it:
describe("Unit Testing Examples", function () {
var myApp;
beforeEach(function () {
myApp = angular.module('myApp');
});
it('should have a View1Ctrl controller', function () {
expect(myApp).toBeDefined();
expect(myApp.controller('View1Ctrl')).toBeDefined();
});
});
There is also a related jsfiddle demonstrating the case.

Related

Getting an Error: [$injector:modulerr] Failed to instantiate module App in angularjs + karma + jasmine project

We have a huge project with multiple controllers, factories,configs etc and recently i started to integrate karma+ jasmine to write unit test cases. However I get the above error. I read multiple solutions and injected the module in multiple ways but still get similar or multiple errors.
I want to test my factories, controllers, config but even following multiple tutorials didn't fixed my issue and I dont understand it either.
Can someone help?
app.js
var App = angular.module('App', ['ngRoute', 'ngCookies','ngMaterial', 'infinite-scroll','ui.multiselect']);
App.factory("LS", ['$window', function($window) {
return {
setData: function(key, val) {
$window.localStorage && $window.localStorage.setItem(key, val);
return this;
},
getData: function(obj) {
return $window.localStorage && $window.localStorage.getItem(obj);
}
};
}]);
app.spec.js
describe('LS factory', function() {
var LS;
beforeEach(module('App'));
beforeEach(inject(function() {
var $injector = angular.injector(['App']);
LS = $injector.get('LS');
}));
it('has a dummy spec to test 2 + 2', function() {
expect(2+2).toEqual(4);
});
});
karma.conf.js:
files: [
'./node_modules/angular/angular.js',
'./node_modules/angular-route/angular-route.min.js', // angular
'./node_modules/angular-cookies/angular-cookies.min.js',
'./node_modules/angular-material/angular-material.min.js',
'./node_modules/ng-infinite-scroll/build/ng-infinite-scroll.min.js',
'./assets/lib/js/multiselect.js',
'./node_modules/angular-ui-router/release/angular-ui-router.js', // ui-router
'./node_modules/angular-mocks/angular-mocks.js',
'./node_modules/angular-animate/angular-animate.min.js',
'./node_modules/angular-aria/angular-aria.min.js',
'./node_modules/jquery/dist/jquery.min.js',
'./node_modules/bootstrap/dist/js/bootstrap.min.js',
'./node_modules/moment/moment.js',
'./node_modules/lodash/lodash.js',
'./node_modules/owl.carousel/dist/owl.carousel.min.js',
'./node_modules/c3/c3.js',
'./app.js',
'./assets/common/*.js',
'./assets/configs/*.js',
'./assets/lib/adb/*.js',
'./admin-portal/*/*.js',
'./advertiser-portal/*/*.js',
'./publisher-portal/*/*.js',
'./app.spec.js'
],
I included all the files in karma.conf.js but still I get the following error:
Chrome 66.0.3359 (Linux 0.0.0) LS factory has a dummy spec to test 2 + 2 FAILED
Error: [$injector:modulerr] Failed to instantiate module App due to:
at <Jasmine>
at node_modules/angular/angular.js:125:12
at node_modules/angular/angular.js:5034:15
at forEach (node_modules/angular/angular.js:418:20)
at loadModules (node_modules/angular/angular.js:4994:5)
at Object.createInjector [as injector] (node_modules/angular/angular.js:4911:19)
at UserContext.WorkFn (node_modules/angular-mocks/angular-mocks.js:3181:52)
at <Jasmine>
Chrome 66.0.3359 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.025 secs / 0.018 secs)
Can someone help me to fix this issue?
Im using angular 1.6.10 and karma 2.0.2!
Apparently one of the dependencies I imported ui.multiselect is causing the error.
I removed all the dependencies and run karma test
So I started checking it by adding one after the other dependency
Finally found the issue is with one of the dependency and removed it. All the test cases are working fine now.

Karma running old test file [AngularJS, Karma, Jasmine, PhantomJS]

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.

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)
}

Resources