问题描述
我正在使用 Jasmine 和 Karma 来测试我基于 Angular 构建的应用.
I'm using Jasmine with Karma to test my app built on Angular.
我必须测试加载用户数据的服务,并且我正在使用 $httpBackend 来模拟响应.但是,当我运行测试时,出现了两个错误:
I've to test a service that loads user data and I'm using $httpBackend to mock the responses.However, when I run the test, I got two errors:
- 错误:没有待处理的刷新请求!
- 错误:未满足的请求:GET https://api.github.com/users/wilk
模块:
'use strict';
app.service ('UserService', ['$resource', '$q', 'GITHUB_API_URL', function ($resource, $q, GITHUB_API_URL) {
var userResource = $resource (GITHUB_API_URL + '/users/:user', {user: '@user'}) ,
userModel = {};
return {
data: function () {
return userModel;
} ,
populate: function (user) {
var deferred = $q.defer () ,
userRequest = userResource.get ({user: user});
$q
.when (userRequest.$promise)
.then (function (data) {
userModel = data;
deferred.resolve (data);
});
return deferred.promise;
}
};
}]);
测试:
'use strict';
describe ('Service: UserService', function () {
beforeEach (module ('myApp'));
var $appInjector = angular.injector (['myApp']) ,
UserService = $appInjector.get ('UserService') ,
GITHUB_API_URL = $appInjector.get ('GITHUB_API_URL') ,
GITHUB_USER = $appInjector.get ('GITHUB_USER') ,
$httpBackend;
beforeEach (inject (function ($injector) {
$httpBackend = $injector.get ('$httpBackend');
$httpBackend
.when ('GET', GITHUB_API_URL + '/users/' + GITHUB_USER)
.respond ({
login: GITHUB_USER ,
id: 618009
});
}));
afterEach (function () {
$httpBackend.verifyNoOutstandingExpectation ();
$httpBackend.verifyNoOutstandingRequest ();
});
describe ('when populate method is called', function () {
it ('should returns user data', function () {
$httpBackend.expectGET (GITHUB_API_URL + '/users/' + GITHUB_USER);
UserService.populate (GITHUB_USER);
$httpBackend.flush ();
expect(UserService.data ()).toEqual ({
login: GITHUB_USER ,
id: 618009
});
});
});
});
假设 GITHUB_API_URL 等于 'https://api.github.com/' 和 GITHUB_USER 等于 'wilk'.
Let's assume that GITHUB_API_URL is equal to 'https://api.github.com/' and GITHUB_USER is equal to 'wilk'.
我正在使用 Karma-Jasmine 0.1.5 和 AngularJS 1.2.6(使用 Angular Mocks 和 Scenario 1.2.6)运行此测试.
I'm running this test with Karma-Jasmine 0.1.5 and AngularJS 1.2.6 (with Angular Mocks and Scenario 1.2.6).
这段代码有什么问题?
推荐答案
我们分别说一下每个错误:
Let's talk about each error separately:
错误:没有待处理的刷新请求!
发生这种情况是因为没有通过 $httpBackend
发出请求,所以没有什么要刷新的.那是因为您在 $httpBackend
之前实例化了 UserService
,所以 Angular 不知道它应该使用它而不是真正的 $http
.如果您查看控制台,您会看到正在发送一个真正的请求.
That's happening because no request was made through $httpBackend
, so there's nothing to flush. That's because you are instantiating UserService
before $httpBackend
and so Angular doesn't know it should use it instead of the real $http
. If you check out the console you'll see that a real request is being sent.
错误:未满足的请求:GET https://api.github.com/users/wilk
同上原因.由于服务未使用 $httpBackend
,因此您创建的期望从未实现.
Same reason as the above. Since $httpBackend
isn't being used by the service, the expectation you've created is never fulfilled.
这是考虑到上述所有内容后重构的规范:
Here's your spec refactored after considering all of the above:
describe ('Service: UserService', function () {
var UserService,
GITHUB_API_URL,
GITHUB_USER,
$httpBackend;
beforeEach(function() {
module('plunker');
inject(function( _$httpBackend_, _UserService_, _GITHUB_API_URL_, _GITHUB_USER_) {
$httpBackend = _$httpBackend_;
UserService = _UserService_;
GITHUB_API_URL = _GITHUB_API_URL_;
GITHUB_USER = _GITHUB_USER_;
});
});
afterEach (function () {
$httpBackend.verifyNoOutstandingExpectation ();
$httpBackend.verifyNoOutstandingRequest ();
});
describe ('when populate method is called', function () {
it ('should returns user data', function () {
$httpBackend
.whenGET(GITHUB_API_URL + '/users/' + GITHUB_USER)
.respond ({
login: GITHUB_USER,
id: 618009
});
UserService.populate(GITHUB_USER);
$httpBackend.flush();
expect(UserService.data().login).toBe(GITHUB_USER);
expect(UserService.data().id).toBe(618009);
});
});
});
注意:我已经稍微改变了注入的方式,但是你的方式很好,只要你在其他一切之前创建 $httpBackend
.
Note: I've changed the way things were being injected a little bit, but the way you are doing is just fine, as long as you create $httpBackend
before everything else.
这篇关于AngularJS+Jasmine:$httpBackend 没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!