我正在尝试用angular和jasmine编写单元测试用例。运行测试时出现以下错误:
/** * 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)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' }) }]);
答案 0 :(得分:0)
我所做的唯一区别是(并且我不会在注入中放入 $ state ):
const stateMock = jasmine.createSpyObj('$state', ['go']);
stateMock.go.and.callFake(() => true);
但是,如果你嘲笑控制器,请不要忘记,即:
vm = _$controller_(SomeController,
{ $state: stateMock }, { });
如果你将它注入真实的控制器,你将需要$ state状态。
回到你的测试,然后这将在$ state上执行你的测试(你可以更进一步,尝试使用HaveBeenCalledWith()代替):
expect(stateMock.go).toHaveBeenCalled();
保留你的最后一行,检查状态名称是否正确,只是为了彻底。