我在工厂中有以下代码:
var app = angular.module('plunker', []);
app.factory('apiFactory', function($http) {
return {
'call':function(){
$http.get('http://www.google.com').then(function(){
return 'I visited Google!';
});
}
};
});
以下的Jasmine测试:
describe('Testing apiFactory', function() {
var $scope,
apiFactory,
$http,
$q,
rootScope;
beforeEach(module('plunker'));
beforeEach(inject(function($rootScope, _apiFactory_, _$http_, _$q_, _$rootScope_) {
apiFactory = _apiFactory_;
$http = _$http_;
$q = _$q_;
rootScope = _$rootScope_;
}));
it('should return "I visited Google!"', function(){
var deferred = $q.defer();
spyOn($http, 'get').and.returnValue(deferred.promise);
deferred.resolve();
apiFactory.call();
rootScope.$apply();
expect($http.get).toHaveBeenCalledWith('http://www.google.com');
});
});
正如您在上面所看到的,我知道如何测试$ http.get承诺已被调用,但我如何测试传递给then()
方法的函数返回"" I访问了Google!"?
以下是此代码的附加信息:http://plnkr.co/edit/DrKGV11eTpi2CrtYtffp
答案 0 :(得分:1)
你几乎就在那里,你只需做几件事。
我将您的测试修改为如下所示:
it('should return "I visited Google!', function(){
var deferred = $q.defer();
var result;
spyOn($http, 'get').and.returnValue(deferred.promise);
deferred.resolve();
apiFactory.call().then(function(resolved) {
result = resolved;
});
expect($http.get).toHaveBeenCalledWith('http://www.google.com');
expect(result).toBeUndefined();
rootScope.$apply();
expect(result).toBe('I visited Google!');
});
为了澄清,您的apiFactory.call
方法应如下所示:
return $http.get('http://www.google.com').then(function(){
return 'I visited Google!';
});
这对你有用!