angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Make controllers used with a bindToController directive easier to test

Open meyertee opened this issue 12 years ago • 31 comments

There doesn't seem to be a clear mechanism of unit-testing a controller that's used in a directive with bindToController: true. These controllers assume certain properties being set before the execution of the constructor. I could build a mechanism myself using a temporary constructor etc. but it would be nice to have something in Angular itself.

I discovered the private boolean arg later in the $controller service that serves the purpose:

ctrlFn = $controller(MyCtrl, {
    $scope: scope,
}, true):
ctrlFn.instance.foo = "bar";
ctrl = ctrlFn();

Would you consider making that flag public and thus officially supported? Or maybe add a helper to angular mock?

See also this Stack Overflow question.

meyertee avatar Oct 04 '14 09:10 meyertee

+1. Testing bindToController should be simple, like the rest of Angular.

ernestoalejo avatar Oct 04 '14 09:10 ernestoalejo

+1

demisx avatar Nov 19 '14 03:11 demisx

+1

azevedo avatar Dec 21 '14 21:12 azevedo

+1

paulstefanday avatar Jan 07 '15 21:01 paulstefanday

+1

Epotignano avatar Jan 19 '15 14:01 Epotignano

Whether you use the flag or not, it's not going to set up bindings for you, the compiler does that. Why not just compile some DOM, then the work is done for free

caitp avatar Jan 19 '15 14:01 caitp

Can you write an example? there are not docs on how to test when use bindToController?

Epotignano avatar Jan 19 '15 14:01 Epotignano

something like this

it('should populate template', inject(function($compile, $rootScope) {
  element = $compile('<div my-directive-with-controller cat-bindings="cats"></div>')($rootScope);
  // edit: use isolateScope() rather than scope(), since this is isolate-scopes-only for now
  newScope = element.isolateScope();
  ctrl = newScope.someId;

  expect(ctrl.catBindings).toBeUndefined();

  $rootScope.$apply('cats = [{name: "Bobby", weight: 6}, {name: "Parker", weight: 7 )]');

  // test that the controller's binding has the right stuff
  expect(ctrl.bindings).toEqual([
    { name: "Bobby", weight: 6 },
    { name: "Parker", weight: 7 }
  ]);

  table = element.find('table');
  rows = element.find('tr');

  // test that the template looks right
  expect(rows.eq(0).find('td').eq(0).text()).toBe('Bobby');
  expect(rows.eq(0).find('td').eq(1).text()).toBe('6.0 kilos');
  expect(rows.eq(1).find('td').eq(0).text()).toBe('Parker');
  expect(rows.eq(1).find('td').eq(1).text()).toBe('7.0 kilos');

  // okay, we're all done!
}));

caitp avatar Jan 19 '15 14:01 caitp

So if I'm not wrong the unit tests would be something like this now:

it('should have a default label', inject(function($compile, $rootScope) {
  element = $compile('<remove-button></remove-button>')($rootScope);
  newScope = element.isolateScope();
  ctrl = newScope.someId;

  newScope.$apply();

  expect(ctrl.label).toBe('Delete');
}));

it('should use the specified label', inject(function($compile, $rootScope) {
  element = $compile('<remove-button label="foo label"></remove-button>')($rootScope);
  newScope = element.isolateScope();
  ctrl = newScope.someId;

  newScope.$apply();

  expect(ctrl.label).toBe('foo label');
}));

ernestoalejo avatar Jan 19 '15 15:01 ernestoalejo

I'm using this approach - seems to work all right.

elkorn avatar Jan 19 '15 15:01 elkorn

Is working well for me.

Epotignano avatar Jan 19 '15 20:01 Epotignano

+1 for making later public.

You wanna test your controller's behavior, not the directive's side-effects.

cesarandreu avatar Feb 10 '15 18:02 cesarandreu

We have intentionally pulled almost all of our logic out of link functions and into directive controllers because controllers are much easier to test. You don't have to build a random DOM fragment and you can easily isolate all of the necessary parameters. Until now. So if we use bindToController we have to take a step back and return to building and parsing DOM fragments by hand, making setup and / or every test more complicated. There has to be a better way to do this... :neutral_face:

SonofNun15 avatar Mar 04 '15 16:03 SonofNun15

+1 for making later public

SonofNun15 avatar Mar 04 '15 16:03 SonofNun15

So what sort of testing API do you actually want? Like, how would you envision this working?

caitp avatar Mar 04 '15 16:03 caitp

Before we build the controller, we currently set stuff on the scope object that we expect from the attribute bindings (isolated scope):

scope = $rootScope.$new();
scope.attribute = "expected param";
var controller = $controller("MyController", { $scope: scope });
return controller;

Now our test can verify that the controller set up its instance properties correctly based on the attribute bindings and / or test controller behavior via methods on the controller.

SonofNun15 avatar Mar 04 '15 16:03 SonofNun15

If we are using bindToController: true, this would ideally change to something like either:

var buildController = $controller("MyController", { }, true);
buildController.instance.attribute = "expected param";
return buildController();

Or, better yet something like:

var controller = $controller("MyController", { }, { attribute: "expected param" });
return controller;

SonofNun15 avatar Mar 04 '15 16:03 SonofNun15

The root of the problem is that the bound attributes need to be set on the controller instance before the controller function is called.

SonofNun15 avatar Mar 04 '15 16:03 SonofNun15

@petebacondarwin what do you think? mock $controller service could have an extra parameter for setting up bindings, I guess?

The limitation is, they wouldn't be real bindings, and wouldn't result in any $watches or anything being set up. Which is great for some kinds of testing, but not so great for others

caitp avatar Mar 04 '15 16:03 caitp

I like that idea. I guess that in most tests you would simply simulate watch driven changes from the outside.

petebacondarwin avatar Mar 04 '15 16:03 petebacondarwin