How to access controllerAs namespace in unit test with compiled element?

送分小仙女□ 提交于 2019-12-01 17:27:21

问题


In this fiddle http://jsfiddle.net/FlavorScape/fp1kktt9/ i try to set properties on the controller, not the $scope directly. In the template (in production) we just do myAliasCtrl.somePropertyList and the ng-repeat works.

However, this does not work in testing. I don't know how to get/assign the property on the controller.

UPDATE: I must have had some weird localized issue in my testing environment, because i do get ng-repeat to work. notice how there's two child elements, even though the property is on the aliased controller. http://jsfiddle.net/FlavorScape/2adn983y/2/

however, my question still is, how would I get a reference to that compiled controller to say, create a spy? (Or just get the reference to the aliased controller)?

Here's the source:

angular.module('myApp', [])
.directive('myTestDirective', function () {
    return {
        restrict: 'E',
        scope: {
            myBinding: '='
        },
        replace: true,
        template: '<div ng-if="isRendered">TEST<div ng-repeat="foo in myCtrl.fooList">{{foo}}</div></div>',
        controller: 'myTestController',
        controllerAs: 'myCtrl'
    };
})
.controller('myTestController', function($scope) {
    $scope.isRendered = true;
     // if i reference this, ng-repeat works
    $scope.fooList = ["boob","cat", "Tesla"];
});

describe('myTest directive:', function () {
    var scope, compile, validHTML;

    validHTML = '<div><my-test-directive my-binding="isRendered"></my-test-directive></div>'; //i

    beforeEach(module('myApp'));

    beforeEach(inject(function($compile, $rootScope){
        scope = $rootScope.$new();        
        scope.isRendered = true;

        compile = $compile;
    }));

    function create() {
        var elem, compiledElem;
        elem = angular.element(validHTML);
        compiledElem = compile(elem)(scope);
        scope.$digest();
        return compiledElem;    
    }

    it('should have a scope on root element', function () {  
        var el = create();

        // how to get the controller???
        el.scope().myCtrl.fooList =  ["monkey","apple","Dishwasher"];

        // notice it just has <!-- ng-repeat
        console.log( el );


        expect(el.text()).toContain('TEST');

    });
});

回答1:


Everything works as expected :) You are just trying to access the wrong scope.

Because ngIf creates a new scope, you should access that scope (because isRendered is created on that child scope:

expect(el.children().first().scope().isRendered).toBeTruthy();

Here is the updated fiddle.


UPDATE:

You are using controllerAs, basically you get the controller's this bound to a scope property. E.g. controllerAs: 'myTestCtrl' implicitly results to $scope.myTestCtrl = this; (where this is the controller instance.

But again you where trying to access the wrong element. You need the first child-element of the wrapping <div> and then you need its isolate scope (not normal scope):

var ctrl = el.children().first().isolateScope().myTestCtrl;

Another updated fiddle



来源:https://stackoverflow.com/questions/25189304/how-to-access-controlleras-namespace-in-unit-test-with-compiled-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!