Testing the angularjs directive with dependencies

I am new to AngularJs and have a problem trying to check the directive with a dependency (although the directive itself works as expected). I could not find the answers to these questions or to other resources.

Here is my 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 to 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 does not work with the message:

 Error: Expected '' to be '0.1'. 

means the configuration value was entered correctly, but $ complile did not use it. I would really appreciate any help with this. Thanks.

+6
source share
1 answer

You did not specify a directive restriction attribute. If you do not specify it, it means that angular is looking for the version of the application declared as an attribute, not an element.

So, you can either add the restriction attribute to the directive or change your template:

 element = $compile('<div app-version></div>')(scope); 
+3
source

Source: https://habr.com/ru/post/949222/


All Articles