我的测试是:
(function() {
describe('Login Controller', function() {
beforeEach(module('myApp'));
beforeEach(inject(function($injector) {
var $controller, $httpBackend, $q, userServiceMock;
self.$rootScope = $injector.get('$rootScope');
self.$scope = $rootScope.$new();
self.$state = $injector.get('$state');
$q = $injector.get('$q');
$httpBackend = $injector.get('$httpBackend');
$controller = $injector.get('$controller');
userServiceMock = {
login: function(auth) {
self.deferred = $q.defer();
console.log("HERE!");
return self.deferred.promise;
}
};
self.createController = function() {
return $controller('LoginController', {
'$scope': self.$scope,
'$rootScope': self.$rootScope,
'$state': self.$state,
'userService': userServiceMock
});
};
return $httpBackend.whenPOST('http://localhost:9001/api/v1/session/check').respond({
authenticated: true
});
}));
it('should set the page title to "Login"', function() {
self.createController();
$scope.init();
expect($rootScope.pageTitle).toBe('Login');
return expect($scope.auth).toEqual({});
});
return it('should properly authenticate a user', function() {
self.createController();
$scope.init();
$scope.auth = {
username: '[email protected]',
password: 'mypassword'
};
$scope.login();
self.deferred.resolve({
authenticated: true
});
$scope.$root.$digest();
console.log($state.current.name);
expect($state.current.name).toBe('dashboard');
});
});
})();
我想这很简单吗?我的控制器是:
myApp.controller('LoginController', [
'$scope', '$rootScope', '$state', 'userService', function($scope, $rootScope, $state, userService) {
$scope.init = function() {
$rootScope.pageTitle = 'Login';
return $scope.auth = {};
};
$scope.login = function() {
return userService.login($scope.auth).then(function(response) {
if (response.authenticated === true) {
return $state.transitionTo('dashboard');
} else {
return console.log('bad password man');
}
});
};
return $scope.init();
}
]);
我在测试中得到的是:
NFO [karma]: Karma v0.12.16 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Mac OS X)]: Connected on socket TRjrAWL8PHdQ-bKqkyzK with id 61166122
LOG: 'HERE!'
PhantomJS 1.9.7 (Mac OS X) - Login Controller:
PhantomJS 1.9.7 (Mac OS X) should set the page title to "Login" PASSED
PhantomJS 1.9.7 (Mac OS X) should properly authenticate a user FAILED
PhantomJS 1.9.7 (Mac OS X): Executed 2 of 2 (1 FAILED) (0.02 secs / 0.018 secs)
PhantomJS 1.9.7 (Mac OS X)
PhantomJS 1.9.7 (Mac OS X) Login Controller should properly authenticate a user FAILED
Error: Unexpected request: GET templates/dashboard.html
No more request expected
发生这种情况的原因是因为状态已转换为dashboard
,然后尝试加载dashboard.html
模板。我不一定要测试。我只需要测试它是否处于正确的状态即可。如何正确设置?
我使用的是Angular UI Router,而不是内置的$routeProvider
您可以模拟出模板文件,从而避免使用HTTP错误$templateCache
:
$templateCache = $injector.get('$templateCache');
$templateCache.put('templates/dashboard.html','<div>blank or whatever</div>');
...另一种方法是$httpBackend
...
顺便说一句,您不必花$injector
时间,只需将可注射物添加到您的inject
函数中即可。
beforeEach(inject(function($rootScope, $state, $httpBackend, $templateCache) {
我经常使用下划线语法来提高灵活性:
beforeEach(inject(function(_$rootScope_, _$state_, _$httpBackend_, _$templateCache_) {
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句