@@ -13,45 +13,44 @@ Unit testing as the name implies is about testing individual units of code. Unit
13
13
answer questions such as "Did I think about the logic correctly?" or "Does the sort function order the list
14
14
in the right order?"
15
15
16
- In order to answer such question it is very important that we can isolate the unit of code under test.
16
+ In order to answer such a question it is very important that we can isolate the unit of code under test.
17
17
That is because when we are testing the sort function we don't want to be forced into creating
18
18
related pieces such as the DOM elements, or making any XHR calls in getting the data to sort.
19
19
20
- While
21
- this may seem obvious it usually is very difficult to be able to call an individual function on a
22
- typical project. The reason is that the developers often mix concerns, and they end up with a
23
- piece of code which does everything. It reads the data from XHR, it sorts it and then it
20
+ While this may seem obvious it can be very difficult to call an individual function on a
21
+ typical project. The reason is that the developers often mix concerns resulting in a
22
+ piece of code which does everything. It makes an XHR request, it sorts the response data and then it
24
23
manipulates the DOM.
25
24
26
25
With Angular we try to make it easy for you to do the right thing, and so we
27
- provide dependency injection for your XHR (which you can mock out) and we created abstraction which
26
+ provide dependency injection for your XHR (which you can mock out) and we created abstractions which
28
27
allow you to sort your model without having to resort to manipulating the DOM. So that in the end,
29
28
it is easy to write a sort function which sorts some data, so that your test can create a data set,
30
29
apply the function, and assert that the resulting model is in the correct order. The test does not
31
- have to wait for XHR, or create the right kind of DOM, or assert that your function has mutated the
32
- DOM in the right way.
30
+ have to wait for the XHR response to arrive, create the right kind of test DOM, nor assert that your
31
+ function has mutated the DOM in the right way.
33
32
34
33
## With great power comes great responsibility
35
34
36
- Angular is written with testability in mind, but it still requires that you
37
- do the right thing. We tried to make the right thing easy, but Angular is not magic, which means if
38
- you don't follow these guidelines you may very well end up with an untestable application.
35
+ Angular is written with testability in mind, but it still requires that you do the right thing.
36
+ We tried to make the right thing easy, but Angular is not magic. If you don't follow these guidelines
37
+ you may very well end up with an untestable application.
39
38
40
39
## Dependency Injection
41
- There are several ways in which you can get a hold of a dependency:
42
- 1. You could create it using the `new` operator.
43
- 2. You could look for it in a well known place, also known as global singleton.
44
- 3. You could ask a registry (also known as service registry) for it. (But how do you get a hold of
45
- the registry? Most likely by looking it up in a well known place. See #2)
46
- 4. You could expect that it be handed to you.
40
+ There are several ways in which you can get a hold of a dependency. You can :
41
+ 1. Create it using the `new` operator.
42
+ 2. Look for it in a well- known place, also known as a global singleton.
43
+ 3. Ask a registry (also known as service registry) for it. (But how do you get a hold of
44
+ the registry? Most likely by looking it up in a well known place. See #2. )
45
+ 4. Expect it to be handed to you.
47
46
48
47
Out of the four options in the list above, only the last one is testable. Let's look at why:
49
48
50
49
### Using the `new` operator
51
50
52
- While there is nothing wrong with the `new` operator fundamentally the issue is that calling a new
53
- on a constructor permanently binds the call site to the type. For example lets say that we are
54
- trying to instantiate an `XHR` so that we can get some data from the server.
51
+ While there is nothing wrong with the `new` operator fundamentally, a problem arises when calling ` new`
52
+ on a constructor. This permanently binds the call site to the type. For example, lets say that we try to
53
+ instantiate an `XHR` that will retrieve data from the server.
55
54
56
55
<pre>
57
56
function MyClass() {
@@ -64,12 +63,12 @@ function MyClass() {
64
63
}
65
64
</pre>
66
65
67
- The issue becomes that in tests, we would very much like to instantiate a `MockXHR` which would
66
+ A problem surfaces in tests when we would like to instantiate a `MockXHR` that would
68
67
allow us to return fake data and simulate network failures. By calling `new XHR()` we are
69
- permanently bound to the actual XHR, and there is no good way to replace it. Yes there is monkey
70
- patching. That is a bad idea for many reasons which are outside the scope of this document.
68
+ permanently bound to the actual XHR and there is no way to replace it. Yes, we could monkey
69
+ patch, but that is a bad idea for many reasons which are outside the scope of this document.
71
70
72
- The class above is hard to test since we have to resort to monkey patching:
71
+ Here's an example of how the class above becomes hard to test when resorting to monkey patching:
73
72
<pre>
74
73
var oldXHR = XHR;
75
74
XHR = function MockXHR() {};
@@ -81,7 +80,7 @@ XHR = oldXHR; // if you forget this bad things will happen
81
80
82
81
83
82
### Global look-up:
84
- Another way to approach the problem is to look for the service in a well known location.
83
+ Another way to approach the problem is to look for the service in a well- known location.
85
84
86
85
<pre>
87
86
function MyClass() {
@@ -95,14 +94,14 @@ function MyClass() {
95
94
}
96
95
</pre>
97
96
98
- While no new instance of the dependency is being created, it is fundamentally the same as `new`, in
99
- that there is no good way to intercept the call to `global.xhr` for testing purposes, other then
97
+ While no new dependency instance is created, it is fundamentally the same as `new` in
98
+ that no way exists to intercept the call to `global.xhr` for testing purposes, other then
100
99
through monkey patching. The basic issue for testing is that a global variable needs to be mutated in
101
- order to replace it with call to a mock method. For further explanation why this is bad see: {@link
100
+ order to replace it with call to a mock method. For further explanation of why this is bad see: {@link
102
101
http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/ Brittle Global
103
102
State & Singletons}
104
103
105
- The class above is hard to test since we have to change global state:
104
+ The class above is hard to test since we have to change the global state:
106
105
<pre>
107
106
var oldXHR = global.xhr;
108
107
global.xhr = function mockXHR() {};
@@ -115,7 +114,7 @@ global.xhr = oldXHR; // if you forget this bad things will happen
115
114
116
115
### Service Registry:
117
116
118
- It may seem as that this can be solved by having a registry for all of the services, and then
117
+ It may seem that this can be solved by having a registry of all the services and then
119
118
having the tests replace the services as needed.
120
119
121
120
<pre>
@@ -131,12 +130,12 @@ function MyClass() {
131
130
}
132
131
</pre>
133
132
134
- However, where does the serviceRegistry come from? if it is:
135
- * `new`-ed up, the test has no chance to reset the services for testing
136
- * global look-up, then the service returned is global as well (but resetting is easier, since
137
- there is only one global variable to be reset).
133
+ However, where does the serviceRegistry come from? If it is:
134
+ * `new`-ed up, the test has no chance to reset the services for testing.
135
+ * a global look-up then the service returned is global as well (but resetting is easier, since
136
+ only one global variable exists to be reset).
138
137
139
- The class above is hard to test since we have to change global state:
138
+ The class above is hard to test since we have to change the global state:
140
139
<pre>
141
140
var oldServiceLocator = global.serviceLocator;
142
141
global.serviceLocator.set('xhr', function mockXHR() {});
@@ -148,7 +147,7 @@ global.serviceLocator = oldServiceLocator; // if you forget this bad things will
148
147
149
148
150
149
### Passing in Dependencies:
151
- Lastly the dependency can be passed in.
150
+ Last, the dependency can be passed in.
152
151
153
152
<pre>
154
153
function MyClass(xhr) {
@@ -161,12 +160,12 @@ function MyClass(xhr) {
161
160
}
162
161
</pre>
163
162
164
- This is the preferred way since the code makes no assumptions as to where the `xhr` comes from,
165
- rather that whoever created the class was responsible for passing it in. Since the creator of the
163
+ This is the preferred method since the code makes no assumptions about the origin of `xhr` and cares
164
+ instead about whoever created the class responsible for passing it in. Since the creator of the
166
165
class should be different code than the user of the class, it separates the responsibility of
167
- creation from the logic, and that is what dependency-injection is in a nutshell.
166
+ creation from the logic. This is dependency-injection is in a nutshell.
168
167
169
- The class above is very testable, since in the test we can write:
168
+ The class above is testable, since in the test we can write:
170
169
<pre>
171
170
function xhrMock(args) {...}
172
171
var myClass = new MyClass(xhrMock);
@@ -176,12 +175,12 @@ myClass.doWork();
176
175
177
176
Notice that no global variables were harmed in the writing of this test.
178
177
179
- Angular comes with {@link di dependency injection} built in which makes the right thing
178
+ Angular comes with {@link di dependency injection} built-in, making the right thing
180
179
easy to do, but you still need to do it if you wish to take advantage of the testability story.
181
180
182
181
## Controllers
183
- What makes each application unique is its logic, which is what we would like to test. If the logic
184
- for your application is mixed in with DOM manipulation, it will be hard to test as in the example
182
+ What makes each application unique is its logic, and the logic is what we would like to test. If the logic
183
+ for your application contains DOM manipulation, it will be hard to test. See the example
185
184
below:
186
185
187
186
<pre>
@@ -209,7 +208,7 @@ function PasswordCtrl() {
209
208
}
210
209
</pre>
211
210
212
- The code above is problematic from a testability point of view, since it requires your test to have the right kind
211
+ The code above is problematic from a testability point of view since it requires your test to have the right kind
213
212
of DOM present when the code executes. The test would look like this:
214
213
215
214
<pre>
@@ -226,8 +225,8 @@ expect(span.text()).toEqual('weak');
226
225
$('body').html('');
227
226
</pre>
228
227
229
- In angular the controllers are strictly separated from the DOM manipulation logic which results in
230
- a much easier testability story as can be seen in this example:
228
+ In angular the controllers are strictly separated from the DOM manipulation logic and this results in
229
+ a much easier testability story as the following example shows :
231
230
232
231
<pre>
233
232
function PasswordCtrl($scope) {
@@ -245,7 +244,7 @@ function PasswordCtrl($scope) {
245
244
}
246
245
</pre>
247
246
248
- and the test is straight forward
247
+ and the test is straight forward:
249
248
250
249
<pre>
251
250
var $scope = {};
@@ -255,11 +254,11 @@ $scope.grade();
255
254
expect($scope.strength).toEqual('weak');
256
255
</pre>
257
256
258
- Notice that the test is not only much shorter but it is easier to follow what is going on . We say
257
+ Notice that the test is not only much shorter, it is also easier to follow what is happening . We say
259
258
that such a test tells a story, rather then asserting random bits which don't seem to be related.
260
259
261
260
## Filters
262
- {@link api/ng.$filterProvider Filters} are functions which transform the data into user readable
261
+ {@link api/ng.$filterProvider Filters} are functions which transform the data into a user readable
263
262
format. They are important because they remove the formatting responsibility from the application
264
263
logic, further simplifying the application logic.
265
264
@@ -282,7 +281,7 @@ you create with directives may be used throughout your application and in many d
282
281
283
282
### Simple HTML Element Directive
284
283
285
- Lets start with an angular app with no dependencies.
284
+ Let's start with an angular app with no dependencies.
286
285
287
286
<pre>
288
287
var app = angular.module('myApp', []);
0 commit comments