0

I am trying to write unit test case with angular and jasmine. I am getting the following error when running the tests:

var BCM = angular.module("App", ['ui.router','ngAnimate','ui.bootstrap','ngAria','ngTable','ngSanitize','jcs-autoValidate','toaster', 'googlechart'])
.config( [ "$stateProvider","$urlRouterProvider","$controllerProvider","$provide","$compileProvider", function($stateProvider, $urlRouterProvider,$controllerProvider,$provide,$compileProvider) {

    $urlRouterProvider.otherwise("/")
    $stateProvider
    .state('login',{
        url: "/",
        templateUrl: "media/views/login/login.html",
        resolve :{
            deps: BCM.resolveScriptDeps(['media/js/app/modules/login/model/login-model.js','media/js/app/modules/login/controllers/login-controller.js'])
        },
        controller  : 'loginController'
    })
}]);
/** * Test cases for app-loader */ 'use strict'; describe('caseInvestigation', function () { var $state, $stateParams, $q, $templateCache, $location, $rootScope,$injector,getAllCase,getNewCase,getOwnCase,getResolvedCase; beforeEach(function(){ angular.mock.module('ui.router'); angular.mock.module('ngAnimate'); angular.mock.module('ui.bootstrap'); angular.mock.module('ngAria'); angular.mock.module('ngTable'); angular.mock.module('ngSanitize'); angular.mock.module('jcs-autoValidate'); angular.mock.module('toaster'); angular.mock.module('googlechart'); module('caseInvetigation') }); beforeEach(inject(function(_$state_, _$stateParams_, _$q_, _$templateCache_, _$location_, _$rootScope_){ $state = _$state_; $stateParams = _$stateParams_; $q = _$q_; $templateCache = _$templateCache_; $location = _$location_; $rootScope = _$rootScope_; spyOn('$state','go'); $templateCache.put('media/views/login/login.html',''); })); //Test whether our state activates correctly it('should activate the state', function() { $state.go('login'); $rootScope.$digest(); expect($state.current.name).toBe('login'); }); }); Error... debug.html:38 Error: [$injector:modulerr] http://errors.angularjs.org/1.5.0/$injector/modulerr?p0=toaster&p1=Error%3A…ocalhost%3A9876%2Fbase%2Fjs%2Flib%2Fangular%2Fangular-mocks.js%3A2799%3A52) at Error (native) at http://localhost:9876/base/js/lib/angular/angular.min.js:6:416 at http://localhost:9876/base/js/lib/angular/angular.min.js:40:60 at n (http://localhost:9876/base/js/lib/angular/angular.min.js:7:355) at g (http://localhost:9876/base/js/lib/angular/angular.min.js:39:135) at Object.fb [as injector] (http://localhost:9876/base/js/lib/angular/angular.min.js:43:164) at Object.workFn (http://localhost:9876/base/js/lib/angular/angular-mocks.js:2799:52)window.__karma__.result @ debug.html:38KarmaReporter.specDone @ adapter.js:243dispatch @ jasmine.js:2026(anonymous function) @ jasmine.js:2000specResultCallback @ jasmine.js:927complete @ jasmine.js:376clearStack @ jasmine.js:673QueueRunner.run @ jasmine.js:1932QueueRunner.execute @ jasmine.js:1910queueRunnerFactory @ jasmine.js:710Spec.execute @ jasmine.js:367fn @ jasmine.js:2561attemptAsync @ jasmine.js:1967QueueRunner.run @ jasmine.js:1922QueueRunner.execute @ jasmine.js:1910queueRunnerFactory @ jasmine.js:710fn @ jasmine.js:2546attemptAsync @ jasmine.js:1967QueueRunner.run @ jasmine.js:1922QueueRunner.execute @ jasmine.js:1910queueRunnerFactory @ jasmine.js:710TreeProcessor.execute @ jasmine.js:2408Env.execute @ jasmine.js:772(anonymous function) @ adapter.js:322loaded @ debug.html:42(anonymous function) @ debug.html:140 debug.html:38 TypeError: Cannot read property 'go' of undefined at Object. (app-loader-test.js:32)

1 Answer 1

1

The only difference I have in ones I do is (and I don't put the $state in the inject):

const stateMock = jasmine.createSpyObj('$state', ['go']);
stateMock.go.and.callFake(() => true);

Don't forget however, if you're mocking a controller, i.e.:

vm = _$controller_(SomeController,
  { $state: stateMock }, { });

You'll need the $state in there, if you inject it into the real controller.

Back to your test, then this will perform your test on the $state (you can go a bit further and try toHaveBeenCalledWith() instead):

expect(stateMock.go).toHaveBeenCalled();

Keep your last line ofc, checking that the state name is correct, just to be thorough.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.