Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat(ngMock): allow mock $controller service to set up controller bindings #11239

Closed
wants to merge 1 commit into from

Conversation

caitp
Copy link
Contributor

@caitp caitp commented Mar 4, 2015

Adds a new mock for the $controller service, in order to simplify testing using the
bindToController feature.

var dictionaryOfControllerBindings = {
  data: [
    { id: 0, phone: '...', name: '...' },
    { id: 1, phone: '...', name: '...' },
  ]
};

// When the MyCtrl constructor is called, `this.data ~= dictionaryOfControllerBindings.data`
$controller(MyCtrl, myLocals, dictionaryOfControllerBindings);

Closes #9425

@caitp
Copy link
Contributor Author

caitp commented Mar 4, 2015

So:

Should it be a shallow copy, as extend() currently does? This works better if the dictionary contains native values.

…dings

Adds a new mock for the $controller service, in order to simplify testing using the
bindToController feature.

```js
var dictionaryOfControllerBindings = {
  data: [
    { id: 0, phone: '...', name: '...' },
    { id: 1, phone: '...', name: '...' },
  ]
};

// When the MyCtrl constructor is called, `this.data ~= dictionaryOfControllerBindings.data`
$controller(MyCtrl, myLocals, dictionaryOfControllerBindings);
```

Closes angular#9425
* @param {Object} locals Injection locals for Controller.
* @param {Object=} bindings Properties to add to the controller before invoking
* the constructor. This is used to simulate the `bindToController` feature
* and simplify certain kinds oftests.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oftests --> of tests

@caitp
Copy link
Contributor Author

caitp commented Mar 5, 2015

@SonofNun15 / @cesarandreu would an API like this work for you guys? or do you prefer having the actual bindings set up

@SonofNun15
Copy link

That looks like exactly what I was hoping for. Bindings are not required from my perspective. Thanks so much!

@cesarandreu
Copy link

@caitp this works for me :D. I don't need bindings.

I've basically gotten rid of big link functions in my directives. In my link function I'll setup watchers and call controller actions, and handle any kind of DOM transformation in as much isolation as possible. The controller is left responsible for tracking state and providing glue to services.

@caitp
Copy link
Contributor Author

caitp commented Mar 5, 2015

okay --- @petebacondarwin if you aren't busy watching the conference, does this look good to you with the typo fixed?

module(function($controllerProvider) {
$controllerProvider.register('testCtrl', function() {
called = true;
expect(this.data).toEqual(data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to be more strict and say toBe here? I know it doesn't really matter but since you asked the question about shallow copying I guess we could make it clear what the result of that was.

@petebacondarwin
Copy link
Member

LGTM, merging with tweaked docs. Do we want to change the unit test WRT #11239 (comment) ?

@caitp caitp closed this in d02d058 Mar 6, 2015
caitp added a commit that referenced this pull request Mar 6, 2015
…dings

Adds a new mock for the $controller service, in order to simplify testing using the
bindToController feature.

```js
var dictionaryOfControllerBindings = {
  data: [
    { id: 0, phone: '...', name: '...' },
    { id: 1, phone: '...', name: '...' },
  ]
};

// When the MyCtrl constructor is called, `this.data ~= dictionaryOfControllerBindings.data`
$controller(MyCtrl, myLocals, dictionaryOfControllerBindings);
```

Closes #9425
Closes #11239
@caitp
Copy link
Contributor Author

caitp commented Mar 6, 2015

LGTM, merging with tweaked docs. Do we want to change the unit test WRT #11239 (comment) ?

sounds good to me

hansmaad pushed a commit to hansmaad/angular.js that referenced this pull request Mar 10, 2015
…dings

Adds a new mock for the $controller service, in order to simplify testing using the
bindToController feature.

```js
var dictionaryOfControllerBindings = {
  data: [
    { id: 0, phone: '...', name: '...' },
    { id: 1, phone: '...', name: '...' },
  ]
};

// When the MyCtrl constructor is called, `this.data ~= dictionaryOfControllerBindings.data`
$controller(MyCtrl, myLocals, dictionaryOfControllerBindings);
```

Closes angular#9425
Closes angular#11239
@thelgevold
Copy link

This is great!
I have added a quick write up with an example of how to use it:
http://www.syntaxsuccess.com/viewarticle/5520197d61d7e9d80a9f52db
Thanks for adding this!

@petebacondarwin
Copy link
Member

Thanks @thelgevold

netman92 pushed a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
…dings

Adds a new mock for the $controller service, in order to simplify testing using the
bindToController feature.

```js
var dictionaryOfControllerBindings = {
  data: [
    { id: 0, phone: '...', name: '...' },
    { id: 1, phone: '...', name: '...' },
  ]
};

// When the MyCtrl constructor is called, `this.data ~= dictionaryOfControllerBindings.data`
$controller(MyCtrl, myLocals, dictionaryOfControllerBindings);
```

Closes angular#9425
Closes angular#11239
netman92 pushed a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make controllers used with a bindToController directive easier to test
7 participants