我正试图为我的一个控制器创建第一个单元测试。
我对控制器的Karma / Jasmine简单测试看起来像
describe('RegistrationController', function() {
beforeEach(module('myApp'));
var $controller, $rootScope;
beforeEach(inject(function(_$controller_, _$rootScope_){
// The injector unwraps the underscores (_) from around the parameter names when matching
$controller = _$controller_;
$rootScope = _$rootScope_;
}));
describe('Registration $scope.submit', function() {
it('submits registration and sets step to 1', function() {
var $scope = $rootScope.$new();
var controller = $controller('RegistrationController', { $scope: $scope });
$scope.submit();
expect($scope.step).toEqual(1);
});
});
});
测试控制器的那部分
$scope.submit = function () {
$scope.step = 1; //wizard started
var userName = $scope.user.name;
var userNick = $scope.user.nick;
$location.path('localization');
}
但是当我运行该单元测试时,user.name
和user.nick
失败并显示错误
TypeError: Cannot read property 'name' of undefined
或
TypeError: Cannot read property 'nick' of undefined
当表格捕获这两个值并在控制器端正常工作时。