如何在AngularJS中测试使用controllerAs的视图

时间:2017-04-07 08:01:52

标签: javascript angularjs unit-testing jasmine

我正在尝试测试我的控制器的视图,但我无法让它工作。 当我尝试将简单模板作为<h1>test</h1>时,它可以正常工作,但当模板使用controllerAs语法时,我会得到Expected undefined to contain 'test'. 我做了一些研究,transcludeControllers应该对此进行处理,但事实并非如此。我的测试看起来像这样:

 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');

        });

我如何让它工作?我正在使用AngularJS 1.6.3和Jasmine / Karma组合。

0 个答案:

没有答案