How to mock angular translate filter in unit tests for directives
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
In my directive templates, I need to use the angular translate filter as such:
Then in my unit test for this directive, I get the error:
Unknown provider: translateFilterProvider
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
I can inject the module pascalprecht.translate, but that is heavy handed. How do I best mock the filter?
回答1:
Below is a simple example of how you can mock the filter.
This does not mock the $translate.instant method. For this you could assign the function to a variable and then assign the angular.identity method to the instantproperty.