Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

docs(mocks): add recipe for creating mocks with DI #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jeffbcross
Copy link

I added a recipe for mocking classes with DI. Maybe I'm doing it wrong, so please give a good review before merging :).

Review on Reviewable

@vojtajina
Copy link
Contributor

* Use the @Inject annotation to tell DI to inject the $Window class into the
* HttpBackend constructor.
*/
@Inject($Window)
Copy link
Contributor

Choose a reason for hiding this comment

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

btw, HttpBackend should ask for XMLHttpRequest, not Window.

Copy link
Author

Choose a reason for hiding this comment

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

@vojtajina Can you give some guidance on how to inject a constructor like XMLHttpRequest without attaching it to another object?

Copy link
Contributor

Choose a reason for hiding this comment

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

function HttpRequest() {
  return window.XMLHttpRequest;
}

class HttpBackend {
  @Inject(HttpRequest)
  constructor(HttpRequest) {
    // ...
  }
}

We need a cleaner way for this. See #21 and #22

@vojtajina vojtajina force-pushed the master branch 2 times, most recently from e46889e to e4c8659 Compare August 23, 2014 16:35
@tusharmath
Copy link

I don't think creating mocks manually, is a scalable solution. Every time there is an update in the original class you might have to update your mocks also. I would rather prefer using Stubs. Use a library like - Sinon to stub your requests that are dependent on the XMLHttpRequest object. Any thoughts, where I could be wrong?

@caitp
Copy link
Contributor

caitp commented Oct 12, 2014

@tusharmath sinon actually provides quite a few mocks itself... mocks and stubs sort of solve different problems. Mocking a class which performs async ops is attractive for testing because it lets your tests have a bit more control, and the ability to run synchronously. Stubs are useful for letting you perform assertions at a certain point, or asserting that the particular subroutine was invoked the way you expect it to be.

Both are great for testing, but they don't solve the same problems

@tusharmath
Copy link

@caitp After seeing a lil bit here and there I finally got your point. Thanks!

* use the $MockWindow implementation when the @Inject annotation specifies
* $Window in HttpBackend.
*/
var injector = new Injector([$MockWindow, HttpBackend]);

Choose a reason for hiding this comment

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

Why is one supposed to pass HttpBackend to the Injector ctor?

@tusharmath
Copy link

Where would modules like - Mockery fit in? Are both of them solving the same problem?

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.

4 participants