You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: approvaltests-util/docs/how_to/LoadersAndSavers.md
+56-67Lines changed: 56 additions & 67 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -170,7 +170,7 @@ Step 5: Now we introduce the new loader function as a parameter to the original
170
170
171
171
Step 6: Update the unit tests to use the new Loader parameter.
172
172
We have now removed the reliance on the database to retrieve the data.
173
-
We still rely on a mail server to send the results.
173
+
Note that we still rely on a mail server to send the results.
174
174
175
175
<!-- Snippet Compare: step0, step0_b -->
176
176
@@ -197,72 +197,61 @@ We still rely on a mail server to send the results.
197
197
}
198
198
</pre>
199
199
200
-
201
-
Step 7: Now we can remove the DataBase as a parameter altogether.
202
-
203
-
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer, Loader<List<Customer>> seniorCustomerLoader) {
204
-
205
-
List seniorCustomers = seniorCustomerLoader.load();
206
-
207
-
// ...
208
-
209
-
}
210
-
211
-
becomes
212
-
213
-
public void sendOutSeniorDiscounts(MailServer, Loader<List<Customer>> seniorCustomerLoader) { ... }
214
-
215
-
and we can now remove that parameter from any function that calls this one - including our test.
200
+
And we can now remove that parameter from any function that calls this one - including our test.
216
201
217
202
This removes the dependency on the database for testing purposes.
218
203
219
204
Why not just use a mock object to do this?
220
205
221
-
Because mocking the object in question may require much more than what we are doing here. In this case we are simply replacing a call to a method with the result of that method - as if it were called. Defining a mock object would require more overhead and initialization.
206
+
Because mocking the object in question may require much more than what we are doing here.
207
+
In this case we are simply replacing a call to a method with the result of that method - as if it were called.
208
+
Defining a mock object would require much more overhead and initialization.
222
209
223
210
But we still have a dependency on the MailServer in the example above.
224
211
225
212
Thanks for pointing that out! We'll now show how to solve that problem using a Saver.
226
213
227
-
Savers
214
+
## Savers
228
215
229
216
Let's continue with the above example:
230
-
231
-
public void sendOutSeniorDiscounts(MailServer mailServer, Loader<List<Customer>> seniorCustomerLoader) {
232
-
233
-
List seniorCustomers = seniorCustomerLoader.load();
234
-
235
-
for (Customer customer : seniorCustomers) {
Here we're sending out an email message. But we don't really care if it gets sent, we just want to make sure it contains the information we expect. Replacing the MailServer object with a Saver is very similar to the process of introducing a Loader.
248
237
249
238
Step 1: Determine the function that we call to save (or in this case, send) the data.
250
239
251
240
public void sendOutSeniorDiscounts(MailServer mailServer, Loader<List<Customer>> seniorCustomerLoader) {
252
241
253
-
List seniorCustomers = seniorCustomerLoader.load();
242
+
List seniorCustomers = seniorCustomerLoader.load();
254
243
255
-
for (Customer customer : seniorCustomers) {
0 commit comments