我是 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 没有使用它。我真的很感激这方面的任何帮助。谢谢。