Unit-test a angular control function in the directive

I have a controller defined in the directive and having a malfunction checking it. Is this possible without globalization or a controller branch from a directive? Can you add a simple example?

+4
source share
1 answer

In your case, you can test the controller of elements by accessing the functions of the controllers from the area of ​​compiled elements.

The easiest way to access the scope of an element is to call the #scope () function on the compiled angular element .

it ('should have a function X on scope', inject(function($rootScope, $compile) { var element = $compile('<div test-directive></div>')($rootScope); expect(element.scope().myFunction).toEqual(jasmine.any(Function)); }); 

Here is a simple example of the following method that is used in jsFiddle .

+3
source

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


All Articles