【发布时间】:2015-08-22 20:20:25
【问题描述】:
我有一个使用如下服务函数的指令:
angular.module('testModule',
['serviceBeingUsed'])
.directive('testDirective', function(serviceBeingUsed) {
return {
restrict: 'AE',
templateUrl: 'testTemplate.tpl.html',
scope: {
boundVar1: "="
},
link: function(scope) {
scope.getRequiredData = function(data){
//gether data using service
serviceBeingUsed.fetchRequiredData(data).then(
function(result){
scope.requiredData = result;
}
);
};
}
};
});
在上述指令中,我注入了我希望使用的服务,并且该服务函数在该指令的“链接”内的范围函数“getRequiredData()”内使用。
我的测试套件是这样设置的:
describe('test suite', function () {
var scope,
$rootScope,
$compile,
$q,
element,
isoScope,
serviceBeingUsed;
beforeEach(module('testModule'));
beforeEach( inject( function(_$rootScope_,
_$q_,
_$compile_,
_serviceBeingUsed_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
serviceBeingUsed = _serviceBeingUsed_;
$q = _$q_;
//This is where we create the directive and it's options.
element = angular.element('<test-directive bound-var1="blabla"></test-directive>');
//We create a new scope from the rootScope.
scope = $rootScope.$new();
//Now we compile the HTML with the rootscope
$compile(element)(scope);
//digest the changes
scope.$digest();
//We retrieve the isolated scope scope of the directive
isoScope = element.isolateScope();
}));
现在我有一个运行并通过的测试,我可以在隔离范围函数“getRequiredData()”上窥探,这个测试看起来像这样:
it('getRequiredData runs', inject(function () {
spyOn(isoScope,"getRequiredData");
isoScope.getRequiredData();
expect(isoScope.getRequiredData).toHaveBeenCalled();
}));
这证明可以测试链接功能但是当尝试测试是否调用服务功能时测试失败并且我不知道为什么,服务的测试如下所示:
it('serviceFunction runs', inject(function () {
spyOn(serviceBeingUsed, "serviceFunction").and.callFake(function() {
var deferred = $q.defer();
var data = "returnedDataDummy";
deferred.resolve(data);
return deferred.promise;
});
isoScope.getRequiredData();
expect(serviceBeingUsed.serviceFunction).toHaveBeenCalled();
}));
如果这里调用了服务函数,如何才能成功测试?
【问题讨论】:
-
能否请您发布您遇到的错误?对我来说,这条线有点令人困惑
angular.module('testModule', ['serviceBeingUsed'])- 你的服务也是一个模块吗?检查这个plunker -
服务也是一个模块,被注入到“angular.module('testModule', ['serviceBeingUsed'])”行中
标签: testing service angularjs-directive jasmine angularjs-service