Wie testen Sie eine Richtlinie-controller mit angularJS-karma-Jasmin?

Ziel:

Schreiben Sie eine bestehen der Prüfung für die waCarousel Richtlinie scope-variable: self.awesomeThings. Erwarten, dass diese Prüfung, wenn self.awsomeThings.length.toBe(3) zu wahr ist?

Frage:

Wie kann ich richtig schreiben diesem test? eher wie bekomme ich Spritzen, ein Richtlinien-controller?


Richtlinie:

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

Dies ist, wie ich es tun würde, für eine typische Ansicht und nicht-Direktive:

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

Wie führe ich diese beiden Begriffe so, dass ich erwarten kann self.awesomeThings.length).toBe(3)?

UPDATE:
Wie testen Sie eine Richtlinie-controller mit angularJS-karma-Jasmin?

Ich denke, Sie könnte so etwas tun, nicht sicher, ob es Ihren Bedürfnissen passt, obwohl: jsfiddle.net/themyth92/goLpjezL

InformationsquelleAutor Matthew Harwood | 2014-12-03

Schreibe einen Kommentar