Tre*_*res 19 angularjs karma-jasmine
按照angularJS $ httpBackend的官方指南,我会做这个测试,但是Karma给我这个错误:
Error: No pending request to flush !
at Function.$httpBackend.flush
测试
'use strict';
describe('profileCtrl', function () {
var scope, $httpBackend;
beforeEach(angular.mock.module('maap'));
beforeEach(angular.mock.inject(function($rootScope, $controller, _$httpBackend_){
$httpBackend = _$httpBackend_;
$httpBackend.when('GET', 'profile').respond([{id: 1, name: 'Bob'}]);
scope = $rootScope.$new();
$controller('profileCtrl', {$scope: scope});
}))
it('should fetch list of users', function(){
$httpBackend.flush();
expectGET(scope.current_user.length).toBe(1);
expect(scope.current_user[0].name).toBe('Bob');
});
Run Code Online (Sandbox Code Playgroud)
});
这个简单的控制器:
'use strict';
angular.module('maap').controller('profileCtrl', function($scope, UserService) {
$scope.current_user = UserService.details(0);
Run Code Online (Sandbox Code Playgroud)
});
Tim*_*nin 30
在_$httpBackend_没有任何冲洗,因为你不使你的测试任何HTTP请求.
您需要调用一些发出http请求的代码.
然后,一旦某个地方的某个地方在您的测试中发出了http请求,您就可以调用该flush方法,以便为已经发出的请求提供响应.
就像是:
it('should fetch list of users', function(){
// Do something that will make an http request
MyService.getAllUser(function ...) // for example
// Then provide a response for the http request that
// has been made when getAllUser was called
$httpBackend.flush();
// Check the expected result.
expect(something).toBe('Bob');
});
Run Code Online (Sandbox Code Playgroud)
fgu*_*len 11
同样的问题发生在我身上,问题不在于我没有提出请求,而是因为我提出的请求与预期的请求不同:
例如,我已经定义了这个期望:
mockBackend.expectGET("http://myapi.com/001").respond("RESULT");
Run Code Online (Sandbox Code Playgroud)
我正在请求其他网址:
http://myapi.com/002
Run Code Online (Sandbox Code Playgroud)
非常混乱的错误消息,没有真正容易与下面的问题相关.
| 归档时间: |
|
| 查看次数: |
25114 次 |
| 最近记录: |