How to check a view that uses controllerAs in AngularJS

I am trying to test the view of my controller, but I cannot get it to work. When I try a simple template as <h1>test</h1>

it works, but when the template uses the syntax controllerAs

I get Expected undefined to contain 'test'.

I have done some research and transcludeControllers

have to deal with it, but it is not. My test looks like this:

 it('should show test section', () => {
            let candidatesCtrl = $controller('CandidatesController', mock);
            candidatesCtrl.loading = false;
            let scope = $rootScope.$new();

            let linkFn = $compile('<h1 ng-if="!candidatesCtrl.loading">test</h1>');

            let view = linkFn(scope,
                undefined, {
                    transcludeControllers: {
                        CandidatesController: {
                            instance: candidatesCtrl
                        }
                    }
                });

            scope.$digest();
            let templateAsHtml = view.html();
            expect(templateAsHtml).toContain('test');

        });

      

How do I get this to work? I am using AngularJS 1.6.3

and Jasmine / Karma commands .

+3
javascript angularjs unit-testing jasmine


source to share


No one has answered this question yet

See similar questions:

32
accessing $ scope from unit test file using vm "ControllerAs" syntax from AngularJS HotTowel

or similar:

7728
How do I redirect to another web page?
7649
How does JavaScript blocking work?
7494
How can I remove a specific element from an array in JavaScript?
7432
How to check if a string contains a substring in JavaScript?
7428
How can I check if an element is hidden in jQuery?
5722
How can I remove a property from a JavaScript object?
4523
Thinking in AngularJS if I have a jQuery background?
3178
AngularJS: Service vs provider vs factory
2560
How can I test a private function or class with private methods, fields or inner classes?
1690
How does data binding work in AngularJS?



All Articles
Loading...
X
Show
Funny
Dev
Pics