mardi 7 juin 2016

AngularJS Directives and ControllerAs jasmine test

I'm trying to test a directive with jasmine, but the controller is always undefined.

angular.module('test.app').directive('testDirective', tDirective);

function tDirective() {
  myController.$inject = ["$scope", "$rootScope"];
  return {
      restrict: 'E',
      template: '<div></div>',
      scope: {
        testAttr: '@'
      },
      controller: myController,
      controllerAs: 'vm',
      bindToController: true
    };

  function myController($scope, $rootScope) {
      var vm = this;
      vm.test = "Hello";
      vm.myfunction = function(){
      }
  }
}

Test:

describe('Unit testing test.app', function() {
    beforeEach(module('test.app'));
    beforeEach(inject(function(_$rootScope_, _$controller_){
        scope = _$rootScope_.$new();
    controller = _$controller_("myController", { $scope: scope });
})

So my question is how do I have to initialize the controller (myController) in the testcase and how can I access to vm?

Aucun commentaire:

Enregistrer un commentaire