简体   繁体   English

如何在Jasmine单元测试中正确访问指令的范围和控制器

[英]How to correctly accessing the scope and controller of a directive in a Jasmine unit test

The gist of my issue is that I don't think I am correctly accessing the scope and controller of my directive in accompanying unit tests. 我的问题的要点是,我认为我没有在随附的单元测试中正确访问指令的范围和控制器。

I have a directive that looks something like this: 我有一个看起来像这样的指令:

app.controller('GalleryDirectiveController', ['$scope', '$element', '$timeout', function($scope, $element, $timeout){
    $scope.panels = [];

    this.addPanel = function(panel){
        // Timeout to help with $scope.$watch in other directives
        return $timeout(function(){ $scope.panels.push(panel); }, 0);
    };
}]);

app.directive('gallery', function(){
    return {
        restrict: 'E',
        controller: 'GalleryDirectiveController'
    };
});

What I am trying to do now is write unit tests that mock a collection of gallery panels that add themselves to the gallery. 我现在想要做的是编写模拟单元库面板集合并将其添加到库的单元测试。 Unfortunately, I don't think I am correctly accessing the controller and scope of the directives in my unit tests, so they never pass. 不幸的是,我认为我无法在单元测试中正确访问指令的控制器和范围,因此它们永远不会通过。

The unit test looks something like this: 单元测试看起来像这样:

describe('app Gallery', function(){
    var gallery;
    var $scope;

    beforeEach(module('app'));

    beforeEach(inject(function($compile, $rootScope){

        var element = angular.element('<gallery/>');
        $compile(element)($rootScope.$new());
        $rootScope.$digest();

        $scope = element.isolateScope() || element.scope();
        gallery = element.controller('gallery');

    }));

    it('should add panels to the gallery', function(){
        for (var i = 0; i < 9; i++) {
            gallery.addPanel({
                $el : $('<div></div>')
            });
        }

        // Each panel is added in a timeout, so I thought
        // the problem might have been a timing issue
        waitsFor(function(){
            return $scope.panels.length !== 0;
        }, 'the panels to be added', 200);

    });
});

$scope.panels.length is always zero. $ scope.panels.length始终为零。 This makes me think that the $scope variable I am setting in my unit test is not the same $scope being modified by the controller. 这使我认为我在单元测试中设置的$ scope变量与控制器修改的$ scope不同。 For what it's worth, other unit tests that check if gallery.addPanel and $scope.panels are defined pass as expected. 值得一提的是,其他用于检查gallery.addPanel和$ scope.panels是否已按预期通过的单元测试。

Hopefully I am just missing something small. 希望我只是想念一些小东西。 My fear is that I may have created a hard-to-test gallery directive. 我担心我可能创建了一个难以测试的Gallery指令。

You can flush the $timeout before setting your expectation. 您可以在设置期望值之前刷新$timeout

ie:- 即: -

var $timeout; //or var flushTimeout;
...
beforeEach(inject(function($compile, $rootScope, _$timeout_){ //<-- inject timeout
        $timeout = _$timeout_; //Save it in outer scope
        //or flushTimeout = _$timeout_.flush


...

and in your test do:- 并在您的测试中:

it('should add panels to the gallery', function(){
    for (var i = 0; i < 9; i++) {
        gallery.addPanel({
            $el : $('<div></div>')
        });
    }
    $timeout.flush(); //or flushTimeout();
    expect($scope.panels.length).toEqual(9);
});

Test Demo 测试演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM