我正在尝试对AngularJs中的控制器进行单元测试,我正在关注AngularJs文档作为指导。 https://docs.angularjs.org/guide/controller
(在上面的链接中)他们在控制器中有这个代码
$scope.spice = "habanero";
和测试中的这段代码
it('should set the default value of spice', function() {
expect($scope.spice).toBe('habanero');
});
在我正在测试的控制器中有一些代码如下:
$scope.sessionViewModel = session;
在我的测试中我写了这个:
it('should set the default value of sessionViewModel', function(){
expect($scope.sessionViewModel).toBe(session);
});
但我收到错误TypeError cannot read property 'sessionViewModel' of undefined
不确定我做错了什么
整个测试文件:
describe('forgotPasswordCtrl function', function() {
describe('forgotPasswordCtrl', function(){
var $scope;
var returnMsg = 'Forgot password response message';
var returnMsgTwo = '/web/tfgm_customer';
beforeEach(module('forgotPasswordApp'));
beforeEach(inject(function($rootScope, $controller){
$scope = $rootScope.$new();
$controller = ('ForgotPasswordCtrl', {$scope: $scope});
}));
it('returns a -forgot password response- message', function(){
expect($scope.globalError).toEqual(returnMsg);
});
it('assigns a url depending on a customer match happens', function(){
expect(window.location.assign).toEqual(returnMsgTwo);
});
it('should set the default value of sessionViewModel', function(){
expect($scope.sessionViewModel).toBe(session);
});
});
});
答案 0 :(得分:0)
尝试更改
$controller = ('ForgotPasswordCtrl', {$scope: $scope});
到
controller = $controller('ForgotPasswordCtrl', {$scope: $scope});