0
我有一個AngularJS 1.5.9服務通過測試,我想測試另一個函數調用相同的服務,在一個單獨的它()。問題是第二個服務函數的結果取決於第一個服務調用的傳遞結果;當服務被重新實例化時,第一個測試的成功結果就會失效。如何測試依賴於以前測試通過的AngularJS服務?
我的問題是根據this question's答案。
我覺得我想要做的就是這樣的事情,其中在第一次測試測試用的服務功能在第二次測試正常使用:
describe('user-service', function() {
var $httpBackend, $q, $rootScope;
var mockUserData = { "d": { "firstName": "Matt", "lastName": "Lenny" };
var mockCatalogueData = { "d": { "title": "A Feast of Crows" };
beforeEach(module('users'));
beforeEach(inject(function (_$httpBackend_,_$q_,_$rootScope_) {
$httpBackend = _$httpBackend_;
$q = _$q_;
$rootScope = _$rootScope_;
$httpBackend.when('GET', /(.*)\/user\/api/).respond(200, mockUserData);
$httpBackend.when('GET', /(.*)\/cat\/api/).respond(200, mockCatalogueData);
}));
it('should return the user object', inject(function (userService) {
var user;
var deferred = $q.defer();
var promise = deferred.promise;
promise.then(function (response) {
user = response;
});
userService.getUserInfo().then(function (response) {
deferred.resolve(response);
});
$rootScope.$digest();
$httpBackend.flush();
expect(user).toEqual(mockUserData.d);
}));
it('should return the catalogue object', inject(function (userService) {
var catalogue;
var deferred = $q.defer();
var promise = deferred.promise;
promise.then(function (response) {
catalogue = response;
});
userService.getUserInfo(); // populates internal userService data
userService.getCatalogueInfo().then(function (response) {
deferred.resolve(response);
});
$rootScope.$digest();
$httpBackend.flush();
expect(user).toEqual(mockCatalogueData.d);
}));
});