This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
docs(mocks): add recipe for creating mocks with DI #20
Open
jeffbcross
wants to merge
1
commit into
angular:master
Choose a base branch
from
jeffbcross:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
# Mocking Classes | ||
|
||
It's useful to mock classes for unit testing, to prevent unwanted side effects | ||
of tests. For example, it would be a bad practice to use the browser's | ||
XMLHttpRequest object in HTTP tests, because tests would be slow and | ||
indeterministic. | ||
|
||
DI supports mocking classes in tests in these steps: | ||
|
||
* Create the real module, e.g. `src/Window.js` with a `$Window` class. | ||
* Create the mock module, e.g. `test/mocks/Window.js`, with a `$MockWindow` | ||
class and use the @Provide annotation to indicate that the class can provide | ||
a `$Window`. | ||
* Create a consumer for the module, e.g. `src/httpBackend.js` and use `@Inject` | ||
annotation to inject the constructed `$Window` class into the $HttpBackend | ||
class. | ||
* Create a unit test, which instantiates the injector with the proper | ||
dependencies. | ||
|
||
|
||
## Example | ||
|
||
In `src/Window.js`: | ||
|
||
```javascript | ||
/** | ||
* A class to abstract the native window object, to be constructed and injected | ||
* by DI. | ||
*/ | ||
class $Window { | ||
constructor () { | ||
/** | ||
* Attach the XMLHttpRequest, so a mock version can be used from the | ||
* $MockWindow class. | ||
*/ | ||
this.XMLHttpRequest = window.XMLHttpRequest; | ||
} | ||
somemethod () {} | ||
} | ||
|
||
/** | ||
* Make the $Window class importable in other modules. | ||
*/ | ||
export {$Window}; | ||
``` | ||
|
||
Implement the real `$Window` inside of `HttpBackend`. | ||
In `src/HttpBackend.js`: | ||
|
||
```javascript | ||
/** | ||
* Import the Inject class to inject the $Window class into the HttpBackend | ||
* constructor. | ||
*/ | ||
import {Inject} from '../node_modules/di/src/annotations'; | ||
/** | ||
* Import the $Window class so DI knows exactly which class we intend to inject. | ||
*/ | ||
import {$Window} from '../src/Window'; | ||
|
||
/** | ||
* Use the @Inject annotation to tell DI to inject the $Window class into the | ||
* HttpBackend constructor. | ||
*/ | ||
@Inject($Window) | ||
class HttpBackend { | ||
/** | ||
* Constructor is given a constructed $Window instance as its only argument. | ||
*/ | ||
constructor($window) { | ||
this.xhr = new $window.XMLHttpRequest(); | ||
} | ||
|
||
open(method, url) { | ||
this.xhr.open(method, url); | ||
} | ||
} | ||
|
||
/** | ||
* Make HttpBackend available for import | ||
*/ | ||
export {HttpBackend}; | ||
``` | ||
|
||
In `test/mocks/Window.js`: | ||
|
||
```javascript | ||
/** | ||
* Import Provide from DI so we can tell it we're providing an alternate | ||
* implementation of $Window. | ||
*/ | ||
import {Provide} from '../../node_modules/di/src/annotations'; | ||
/* | ||
* Import $Window so we can tell provide specifically which class we're | ||
* mocking. | ||
*/ | ||
import {$Window} from '../../src/Window'; | ||
|
||
/** | ||
* Use @Provide annotation to say the following class will provide an alternate | ||
* implementation of the specified class | ||
*/ | ||
@Provide($Window) | ||
class $MockWindow { | ||
constructor() { | ||
/** | ||
* Provide a dummy function for XHR. | ||
* In reality, this should be a more sophisticated constructor that would | ||
* allow mimicking the behavior of XHR. | ||
*/ | ||
this.XMLHttpRequest = function () {}; | ||
} | ||
} | ||
|
||
/** | ||
* Export $MockWindow so it can be imported in other modules, such as tests. | ||
*/ | ||
export {$MockWindow}; | ||
``` | ||
|
||
To test HttpBackend, we want to use a mock window service with a mock XHR | ||
constructor. | ||
|
||
In `test/HttpBackend.spec.js`: | ||
|
||
```javascript | ||
/** | ||
* Import the HttpBackend class, the class being tested in this suite of tests. | ||
*/ | ||
import {HttpBackend} from '../src/httpBackend'; | ||
/** | ||
* Import the DI Injector class, of which we'll manually create an instance in | ||
* order to provide the mock implementation of $Window. | ||
*/ | ||
import {Injector} from '../node_modules/di/src/injector'; | ||
/** | ||
* Import the $MockWindow class so we can provide it to the Injector. | ||
*/ | ||
import {$MockWindow} from './mocks/Window'; | ||
|
||
describe('HttpBackend', function () { | ||
it('should construct', function () { | ||
/** | ||
* Create an instance of the Injector, giving it the classes it needs for | ||
* this test. | ||
* Since the $MockWindow class used the @Provide annotation to say it can | ||
* provide an implementation of $Window, the Injector will automatically | ||
* 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 commentThe reason will be displayed to describe this comment to others. Learn more. Why is one supposed to pass |
||
var httpBackend = injector.get(HttpBackend); | ||
expect(httpBackend).toBeInstanceOf(HttpBackend); | ||
expect(new HttpBackend(injector.get($MockWindow))).toBeInstanceOf(HttpBackend); | ||
/** | ||
* httpBackend.open() should throw because it calls xhr.open(), where xhr | ||
* is an instance of an empty function, resuling in a TypeError when calling | ||
* xhr.open. | ||
*/ | ||
expect(function () { | ||
httpBackend.open('get', 'foo') | ||
}).toThrow(); | ||
}); | ||
}); | ||
|
||
|
||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We need a cleaner way for this. See #21 and #22