i'm new angularjs , having problem trying test directive dependency (although directive works expected). unable find answers here or on other resources.
here code:
directive:
angular.module('myapp') .directive('appversion', ['config', function (config) { return function (scope, elm) { elm.text(config.version); }; }]);
service (value):
angular.module('myapp') .value('config', { version: '0.1' });
test:
describe('directive: appversion', function () { beforeeach(module('myapp')); var element; it('should have element text set config value', inject(function ($rootscope, $compile, config) { var scope = $rootscope; element = $compile('<app-version></app-version>')(scope); expect(element.text()).tobe(config.version); })); });
my test failing message:
error: expected '' '0.1'.
meaning config value got injected properly, $complile not using it. appreciate on this. thanks.
you didn't specify restrict attribute of directive. when don't specify it, means angular looks app-version declared attribute, not element.
so can either add restrict attribute directive or change template :
element = $compile('<div app-version></div>')(scope);
Comments
Post a Comment