jeudi 6 octobre 2016

how to add dependencies to $componentController in angular 1.5 while unit testing

I want to add dependencies to my Controller in a component:

Here is my component:

angular
  .module('app')
  .component('myComponent', {
    template: '<div>some content</div>',
    controller: ["$routeParams", function ($routeParams) {
       var ctrl = this;
       ctrl.myId = $routeParams.id;
  });

Now I just want to test this as follow:

describe("spec for myComponent", function () {

  var $componentController;
  var $routeParams;

  beforeEach(module('app'));
  beforeEach(inject(function (_$componentController_, _$routeParams_) {
    $componentController = _$componentController_;
    $routeParams = _$routeParams_;
    $routeParams = {
      myId: 42
    }
  }));

  it('should init', function () {

    var ctrl = $componentController('myComponent', $routeParams, null);

    expect(ctrl.myId).toBe(42);

  });
});

But unfortunately ctrl.myId is undefined because $routeParams are not correctly injected. Why?

Aucun commentaire:

Enregistrer un commentaire