我添加了一个系统,每当URL更改用户是否未登录时都会测试,如果是,则重定向到登录页面。
我有一个测试失败,因为我添加了一些新代码。
代码:
app.run(function($rootScope, userService, authService) {
$rootScope.$on('$routeChangeStart', function () {
if (!userService.isLoggedIn()) {
authService.login();
return;
}
})
});
测试:
describe('profile module', function() {
var $scope, $location;
beforeEach(module('app'));
beforeEach(inject(function($rootScope, $controller, _$httpBackend_) {
$scope = $rootScope.$new();
$location = jasmine.createSpyObj('$location', [ 'path' ]);
$controller('ProfileCtrl', {
$scope : $scope,
$location : $location
});
}));
it('should logout and redirect to login page when disconnect', inject(function($httpBackend) {
$scope.disconnect();
$httpBackend.expectGET('/logout').respond(200);
$httpBackend.flush();
// Not works since new code !
expect($location.path).toHaveBeenCalledWith('/login');
}));
});
测试输出:
.........................
PhantomJS 1.9.8 (Mac OS X 0.0.0) profile module should logout and redirect to login page when disconnect FAILED
Expected spy $location.path to have been called with [ '/login' ] but it was never called.
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 26 of 26 (1 FAILED) (0.069 secs / 0.325 secs)
Warning: Task "karma:unit" failed. Use --force to continue.
我不明白这个问题。 你有什么主意吗? 提前谢谢。