isolateScope() returns undefined when using templateUrl

前端 未结 5 1534
遥遥无期
遥遥无期 2021-02-07 03:41

I have a directive that I want to unittest, but I\'m running into the issue that I can\'t access my isolated scope. Here\'s the directive:

&l         


        
5条回答
  •  借酒劲吻你
    2021-02-07 04:18

    I had to mock and flush the $httpBackend before isolateScope() became defined. Note that $scope.$digest() made no difference.

    Directive:

    app.directive('deliverableList', function () {
        return {
            templateUrl: 'app/directives/deliverable-list-directive.tpl.html',
            controller: 'deliverableListDirectiveController',
            restrict = 'E',
            scope = {
                deliverables: '=',
                label: '@'
            }
        }
    })
    

    test:

    it('should be defined', inject(function ($rootScope, $compile, $httpBackend) {
        var scope = $rootScope.$new();
    
        $httpBackend.expectGET('app/directives/deliverable-list-directive.tpl.html').respond();
    
        var $element = $compile('')(scope);
    
        $httpBackend.flush();
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    
        expect($element).toBeDefined();
        expect($element.controller).toBeDefined();
    
        scope = $element.isolateScope();
        expect(scope).toBeDefined();
        expect(scope.label).toEqual('test');
        expect(scope.deliverables instanceof Array).toEqual(true);
        expect(scope.deliverables.length).toEqual(1);
        expect(scope.deliverables[0]).toEqual({id: 123});
    }));
    

    I'm using Angular 1.3.

提交回复
热议问题