File tree Expand file tree Collapse file tree 2 files changed +6
-10
lines changed Expand file tree Collapse file tree 2 files changed +6
-10
lines changed Original file line number Diff line number Diff line change @@ -14,10 +14,6 @@ using a different approach.
14
14
What is the problem?
15
15
--------------------
16
16
17
- The [ example] ( https://docs.angularjs.org/guide/unit-testing#dependency-injection )
18
- that Angular documentation uses to justify Dependency Injection is the
19
- following:
20
-
21
17
``` javascript
22
18
function doWork () {
23
19
const xhr = new XHR ();
@@ -42,7 +38,7 @@ This small example shows two important concepts. We need a way to get a
42
38
reference to ` XHR ` and a way to provide two implementations: one for the normal
43
39
execution and one for testing.
44
40
45
- In this case, the solution to both concepts is to use the global object. It
41
+ In this case, the solution swaps implementations on the global object. It
46
42
works, but it's not ideal for reasons outlined in this article:
47
43
[ Brittle Global State & Singletons] ( http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/ ) .
48
44
@@ -60,7 +56,7 @@ function doWork(XHR) {
60
56
}
61
57
```
62
58
63
- It makes it very easy to write a test – you just pass your mocked version as
59
+ It makes it very easy to write a test – you pass your mocked version as
64
60
argument to your function:
65
61
66
62
``` javascript
Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ function fetchCurrentUser(callback) {
27
27
return $ .ajax ({
28
28
type: ' GET' ,
29
29
url: ' http://example.com/currentUser' ,
30
- done : user => callback (parseJSON (user)),
30
+ success : user => callback (parseJSON (user)),
31
31
});
32
32
}
33
33
@@ -76,9 +76,9 @@ version of the real module – so we need to tell Jest not to mock the file we
76
76
want to test or else ` require('../fetchCurrentUser') ` will return a mock.
77
77
78
78
In our first test, we want to confirm that calling ` fetchCurrentUser() `
79
- properly incurs a call into ` $.ajax() ` with the parameters we expect. To do
80
- this, we just call ` fetchCurrentUser() ` with a dummy callback function, and
81
- then simply inspect the ` $.ajax ` mock to verify that it was called with the
79
+ properly invokes ` $.ajax() ` with the parameters we expect. To do
80
+ this, we call ` fetchCurrentUser() ` with a dummy callback function, and
81
+ then inspect the ` $.ajax ` mock to verify that it was called with the
82
82
correct parameters.
83
83
84
84
Woohoo! We've written our first test. But we're not quite done: We would still
You can’t perform that action at this time.
0 commit comments