javascript 如何使用 angularJS-karma-jasmine 测试指令的控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27264016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:14:56  来源:igfitidea点击:

How to test a directive's controller using angularJS-karma-jasmine?

javascriptangularjsangularjs-directivejasminekarma-jasmine

提问by Matthew Harwood

Goal:

目标:

Write a passing test for the waCarouseldirective scope variable: self.awesomeThings. Expect this test pass when self.awsomeThings.length.toBe(3)to is true?

waCarousel指令范围变量编写一个通过测试:self.awesomeThings. 期望此测试通过何时self.awsomeThings.length.toBe(3)为 true?

Question:

问题:

How can I properly write this test? rather how do I inject a directives controller?

我该如何正确编写此测试?而是如何注入指令控制器?



Directive:

指示:

angular.module('carouselApp')
    .directive('waCarousel', function() {
        return {
            templateUrl: '../../../views/carousel/wa.carousel.html',
            controller: function($scope) {
                var self = this;

                self.awesomeThings = [1, 2, 3];

                return $scope.carousel = self;
            }
        }
    });

Unit Test:

单元测试:

describe('waCarousel Unit', function() {
  // am I missing a $controller & namespace variable init?
   var $compile,
      $rootScope;


  // Load the myApp module, which contains the directive
  beforeEach(module('carouselApp'));

  // Store references to $rootScope and $compile and $controller
  // so they are available to all tests in this describe block
  beforeEach(inject(function(_$compile_, _$rootScope_, _$controller_){
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $compile = _$compile_;
    $rootScope = _$rootScope_;
    $controller = _$controller_;
     //   WaCarouselCtrl = $controller('WaCarouselCtrl', {
     //       $scope: scope
     //   });
  }));

  it('should have a list of awesomeThings', function() {
    // This wont pass       
    expect(scope.awesomeThings.length).toBe(3);
  });
});

This is how I would do it for a typical view and not directive:

这是我将如何为典型视图而不是指令执行此操作:

describe('Controller: MainCtrl', function() {

    // load the controller's module
    beforeEach(module('carouselApp'));

    var MainCtrl,
        scope;

    // Initialize the controller and a mock scope
    beforeEach(inject(function($controller, $rootScope) {
        scope = $rootScope.$new();
        // !!*** this is how I would inject the typical controller of a view **!! //
        MainCtrl = $controller('MainCtrl', {
            $scope: scope
        });
    }));

    it('should attach a list of awesomeThings to the scope', function() {
        expect(scope.awesomeThings.length).toBe(3);
    });
});

How do I merge these two concepts so that I can expect self.awesomeThings.length).toBe(3)?

我如何合并这两个概念以便我可以期待self.awesomeThings.length).toBe(3)

UPDATE: enter image description here

更新: 在此处输入图片说明

回答by alecxe

Compilethe element, and after calling $digest(), you will have access to the scope which contains carouselobject with awesomeThingsarray:

编译元素,在调用之后$digest(),您将可以访问包含carousel带有awesomeThings数组的对象的范围:

describe('waCarousel Unit', function() {
    var scope;

    beforeEach(module('carouselApp'));
    beforeEach(inject(function($rootScope, $compile) {
        var element = '<test></test>';

        scope = $rootScope.$new();

        element = $compile(element)(scope);
        scope.$digest();
    }));

    it('should have a list of awesomeThings', function() {    
        expect(scope.carousel.awesomeThings.length).toBe(3);
    });
});

Also, here are some useful links to testing directives in angular:

此外,这里有一些有用的链接,可以在 angular 中测试指令: