问题:我如何伪造我的 pointFactory 以便我可以对其进行 Jasmine 单元测试。
我有以下指令。它需要 html 将其发送到工厂并将响应用于某些逻辑
CommonDirectives.directive('TextEnrichment',['PointFactory','appSettings', function (pointFactory,settings) {
return {
restrict: 'A',
link : function (scope, element, attrs) {
var text = element.html();
pointFactory.getPoints(text).then(function(response){
})}}}]);
到目前为止,我的单元测试看起来像这样,但是它不起作用,因为我没有注入工厂。
beforeEach(module('app.common.directives'));
beforeEach(function () {
fakeFactory = {
getPoints: function () {
deferred = q.defer();
deferred.resolve({data:
[{"Text":"Some text"}]
});
return deferred.promise;
}
};
getPointsSpy = spyOn(fakeFactory, 'getPoints')
getPointsSpy.andCallThrough();
});
beforeEach(inject(function(_$compile_, _$rootScope_,_$controller_){
$compile = _$compile_;
$rootScope = _$rootScope_;
}));
it('Factory to have been Called', function () {
var element = $compile('<div data-text-enrichment=""> Text </div>')($rootScope)
expect(getPointsSpy.callCount).toBe('1');
});
更新
根据 Felipe Skinner 的建议,我已使用以下内容更新了测试
beforeEach(function(){
module(function($provide){
$provide.factory('PointFactory',getPointsSpy)
})
});
但是我收到以下错误:
TypeError:“未定义”不是函数(评估“pointFactory.getPoints(文本)”)