-
Notifications
You must be signed in to change notification settings - Fork 95
docs(mocks): add recipe for creating mocks with DI #20
base: master
Are you sure you want to change the base?
Conversation
Can you write it using https://github.com/angular/di.js/blob/master/example/testing/coffee.spec.js ? |
* Use the @Inject annotation to tell DI to inject the $Window class into the | ||
* HttpBackend constructor. | ||
*/ | ||
@Inject($Window) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e46889e
to
e4c8659
Compare
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? |
@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 |
@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]); |
There was a problem hiding this comment.
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?
Where would modules like - Mockery fit in? Are both of them solving the same problem? |
I added a recipe for mocking classes with DI. Maybe I'm doing it wrong, so please give a good review before merging :).