1
votes

Controller

function SomeController(someService) {
  const $ctrl = this;

  activate();

  function activate() {
    someService.retrieveSomeData($ctrl.criteria).then(doSomething);
  }

  function doSomething(response) {
    $ctrl.result = response.data;
  }
}

Test

describe('SomeController', () => {

  beforeEach(module('myModule'));

  let $controller;

  beforeEach(inject((_$controller_,) => {
    $controller = _$controller_;
  }));

 it('tests $controller properties', () => {
    const $scope = {};
    const controller = $controller('SomeController', { $scope });
  });
});

Here I want to sent $ctrl.criteria while initializing my controller so I can test $ctrl.result afterwards.

2
you could just have const controller = $controller('SomeController') where controller will have this available - Pankaj Parkar
As you can see, the activate() method executes on initialization, I tried setting it after initialization, but the activate() method is already fired by then. - Abubakr Dar
@AbubakrDar is this a component's controller? - Siddharth Pandey
No, its not @SiddharthPandey. It is used to show a customized $mdDialog. - Abubakr Dar

2 Answers

0
votes

try this:

describe('SomeController', () => {

  beforeEach(module('myModule'));

  let $controller;

  beforeEach(inject((_$controller_,) => {
    $controller = _$controller_;
  }));

 it('tests $controller properties', () => {
    const scope = {
      criteria : {id: 1}
    };
    const controller = $controller('SomeController', { $scope : scope });
  });
});
0
votes

I'm not sure, if its a general solution or specific to this case, but to pass in the locals of a controller, you have the third aurgument of $controller. At least, it worked for me.

Test

describe('SomeController', () => {

  beforeEach(module('myModule'));

  let $controller;

  beforeEach(inject((_$controller_,) => {
    $controller = _$controller_;
  }));

 it('tests $controller properties', () => {
    const $scope = {};
    const criteria = {id: 1};
    const controller = $controller('SomeController', { $scope }, { criteria });
  });
});