【发布时间】:2017-06-26 13:53:30
【问题描述】:
我有一个非常基本的工厂服务,我想对其进行单元测试。我在expect 语句之前尝试了$rootScope.$digest() 和$rootScope.$apply(),并且在单元测试中根本没有调用成功或失败处理程序 - 在使用该工厂的控制器的上下文中,它们在应用程序中被调用得很好,不过。
example.service.js:
(function (angular) {
'use strict';
angular
.module('exampleModule')
.factory('ExampleApi', ExampleApi);
ExampleApi.$inject = ['$http'];
function ExampleApi($http) {
return {
get: getExampleData
}
function getExampleData() {
return $http.get('example-endpoint');
}
}
})(angular);
example.service.spec.js
'use strict';
describe('Example API service', function() {
var $httpBackend;
var $rootScope;
var ExampleApi;
beforeEach(module('exampleModule'));
beforeEach(inject(
function(_$httpBackend_, _$rootScope_, _ExampleApi_) {
$httpBackend = _$httpBackend_;
$rootScope = _$rootScope_;
ExampleApi = _ExampleApi_;
}));
it('calls the success handler on success', function() {
$httpBackend
.expectGET('example-endpoint')
.respond(200);
var handlerStubs = {
success: function() {
console.log("success called");
},
failure: function() {
console.log("failure called");
}
}
spyOn(handlerStubs, 'success').and.callThrough();
spyOn(handlerStubs, 'failure').and.callThrough();
ExampleApi.get().then(handlerStubs.success, handlerStubs.failure);
//$rootScope.$digest();
$rootScope.$apply();
expect(handlerStubs.failure.calls.count()).toEqual(0);
expect(handlerStubs.success.calls.count()).toEqual(1);
});
});
【问题讨论】:
-
在
response(200);之后尝试$httpBackend.flush(); -
@LukeHutton 它在
response(200)之后没有工作,但在ExampleApi.get()语句之后它确实有效。如果您将其写为答案,我会将其标记为正确答案。非常感谢! (在response行之后,它说Error: No pending request to flush !这就是为什么我想在get()之后尝试它。)
标签: javascript angularjs unit-testing jasmine angular-promise