问题描述
在我的指令模板中,我需要使用 angular translate 过滤器:
In my directive templates, I need to use the angular translate filter as such:
<label for="data-source-btn">
<span id="data-source-btn-span"></span>
{{'Data Source' | translate}}
</label>
然后在我对该指令的单元测试中,我得到错误:
Then in my unit test for this directive, I get the error:
未知提供者:translateFilterProvider
我尝试通过 $translate = $filter('translate');
注入 $filter
并获取 $translate
t 解决问题 - 这真的是为了测试过滤器
I've tried injecting $filter
and getting $translate
by $translate = $filter('translate');
which doesn't solve the problem - this is really for testing the filter
我可以注入pascalprecht.translate
模块,但这很麻烦.我如何最好地模拟过滤器?
I can inject the module pascalprecht.translate
, but that is heavy handed. How do I best mock the filter?
推荐答案
下面是一个如何模拟过滤器的简单示例.
Below is a simple example of how you can mock the filter.
var mockTranslateFilter;
beforeEach(function() {
module(function($provide) {
$provide.value('translateFilter', mockTranslateFilter);
});
mockTranslateFilter = function(value) {
return value;
};
});
这篇关于如何在指令的单元测试中模拟角度转换过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!