我创建了以下小型控制器,用于测试是否以指定的时间间隔触发间隔功能
angular.module('app').controller('myTstController', [
'$interval', function($interval) {
var called = 0;
$interval(function() {
called++;
}, 10);
this.getCalled = function() { return called; }
}
]);
规范:
it('should call getSystemMessages on an interval', function() {
var ctrl, $interval;
angular.mock.module('app');
angular.mock.inject(function($controller, _$interval_) {
$interval = _$interval_;
ctrl = $controller('myTstController', {$interval: $interval});
});
expect(ctrl.getCalled()).toEqual(0);
$interval.flush(10);
expect(ctrl.getCalled()).toEqual(1);
});
Jasmine抛出以下错误:
Error: Unexpected request: GET app/login/login.html
No more request expected
at $httpBackend (http://localhost:39474/Scripts/angular-mocks.js:1175:5)
at sendReq (http://localhost:39474/Scripts/angular.js:7817:9)
at serverRequest (http://localhost:39474/Scripts/angular.js:7551:9)
at wrappedCallback (http://localhost:39474/Scripts/angular.js:10965:15)
at Anonymous function (http://localhost:39474/Scripts/angular.js:11051:11)
at Scope.prototype.$eval (http://localhost:39474/Scripts/angular.js:11977:9)
at Scope.prototype.$digest (http://localhost:39474/Scripts/angular.js:11803:15)
at Scope.prototype.$apply (http://localhost:39474/Scripts/angular.js:12083:13)
at tick (http://localhost:39474/Scripts/angular-mocks.js:497:25)
at $interval.flush (http://localhost:39474/Scripts/angular-mocks.js:546:9)
对我来说,看起来我们遇到了将应用程序重定向到登录页面的错误,但根据我们的代码,我无法理解为什么会发生这种情况?如果我使用flush< 10它将在最后的预期声明中按预期失败。
在这种情况下,有谁知道发生了什么或我们做错了什么?
答案 0 :(得分:1)
注入$httpBackend
并嘲笑
$httpBackend.expectGET('app/login/login.html').respond(200);
解决了这个问题!