我是AngularJs的新手,并且在尝试使用依赖项测试指令时遇到问题(尽管指令本身按预期工作)。我无法在这里或其他资源上找到任何答案。
这是我的代码:
指令:
angular.module('MyApp')
.directive('appVersion', ['config', function (config) {
return function (scope, elm) {
elm.text(config.version);
};
}]);
服务(价值):
angular.module('MyApp')
.value('config', {
version: '0.1'
});
测试:
describe('Directive: AppVersion', function () {
beforeEach(module('MyApp'));
var element;
it('should have element text set to config value', inject(function ($rootScope, $compile, config) {
var scope = $rootScope;
element = $compile('<app-version></app-version>')(scope);
expect(element.text()).toBe(config.version);
}));
});
我的测试失败并显示消息:
Error: Expected '' to be '0.1'.
意味着配置值被正确注入,但$ complile没有使用它。我真的很感激任何帮助。感谢。
答案 0 :(得分:3)
您没有指定指令的restrict属性。 如果不指定它,则表示将声明为属性的app-version的角度查找,而不是元素。
因此,您可以将restrict属性添加到指令或更改模板:
element = $compile('<div app-version></div>')(scope);