|
18 | 18 | This is a protocol for a `URLSession`-like object that can make |
19 | 19 | various network requests. |
20 | 20 |
|
21 | | - @Code(name: "NetworkInterface", file: "NetworkInterface-01.swift") |
| 21 | + @Code(name: "NetworkInterface.swift", file: "NetworkInterface-01.swift") |
22 | 22 | } |
23 | 23 |
|
24 | 24 | @Step { |
25 | 25 | Given this `NetworkInterface`, you might write `RecipeService` |
26 | 26 | like so: |
27 | 27 |
|
28 | | - @Code(name: "RecipeService", file: "RecipeService-01.swift") |
| 28 | + @Code(name: "RecipeService.swift", file: "RecipeService-01.swift") |
29 | 29 | } |
30 | 30 |
|
31 | 31 | @Step { |
|
41 | 41 | <doc:DependencyInjection>) to your Subject. You decide to come up with this |
42 | 42 | first attempt at `FakeNetworkInterface`. |
43 | 43 |
|
44 | | - @Code(name: "FakeNetworkInterface", file: "FakeNetworkInterface-01.swift") |
| 44 | + @Code(name: "FakeNetworkInterface.swift", file: "FakeNetworkInterface-01.swift") |
45 | 45 | } |
46 | 46 |
|
47 | 47 | @Step { |
|
50 | 50 | used. This is where ``Spy`` comes in. Let's rewrite `FakeNetworkInterface` using |
51 | 51 | ``Spy``. |
52 | 52 |
|
53 | | - @Code(name: "FakeNetworkInterface", file: "FakeNetworkInterface-02.swift") |
| 53 | + @Code(name: "FakeNetworkInterface.swift", file: "FakeNetworkInterface-02.swift") |
54 | 54 | } |
55 | 55 |
|
56 | 56 | @Step { |
57 | 57 | This is the start of everything we need to write tests for |
58 | 58 | `RecipeService`, so let's do just that. |
59 | 59 |
|
60 | | - @Code(name: "RecipeServiceTests", file: "RecipeServiceTests-01.swift") |
| 60 | + @Code(name: "RecipeServiceTests.swift", file: "RecipeServiceTests-01.swift") |
61 | 61 | } |
62 | 62 | } |
63 | 63 | } |
|
74 | 74 | @Step { |
75 | 75 | First, let's start with updating `NetworkInterface`. |
76 | 76 |
|
77 | | - @Code(name: "NetworkInterface", file: "NetworkInterface-02.swift") |
| 77 | + @Code(name: "NetworkInterface.swift", file: "NetworkInterface-02.swift") |
78 | 78 | } |
79 | 79 |
|
80 | 80 | @Step { |
81 | 81 | With the updated `NetworkInterface`, we also have to update |
82 | 82 | `RecipeService` to handle these new errors. For our purposes, |
83 | 83 | let's just rethrow the errors and not do anything. |
84 | 84 |
|
85 | | - @Code(name: "RecipeService", file: "RecipeService-02.swift") |
| 85 | + @Code(name: "RecipeService.swift", file: "RecipeService-02.swift") |
86 | 86 | } |
87 | 87 |
|
88 | 88 | @Step { |
|
94 | 94 | `ThrowingSpy` is less boilerplate, and therefore easier to |
95 | 95 | understand, so we'll go with that. |
96 | 96 |
|
97 | | - @Code(name: "FakeNetworkInterface", file: "FakeNetworkInterface-03.swift") |
| 97 | + @Code(name: "FakeNetworkInterface.swift", file: "FakeNetworkInterface-03.swift") |
98 | 98 | } |
99 | 99 |
|
100 | 100 | @Step { |
|
104 | 104 | that we're not crashing on errors. A downstream object might |
105 | 105 | take the error and present an alert with a retry option. |
106 | 106 |
|
107 | | - @Code(name: "RecipeServiceTests", file: "RecipeServiceTests-02.swift") |
| 107 | + @Code(name: "RecipeServiceTests.swift", file: "RecipeServiceTests-02.swift") |
108 | 108 | } |
109 | 109 | } |
110 | 110 | } |
|
125 | 125 | As with before, let's start with `NetworkInterface`, making use |
126 | 126 | of Swift Concurrency for this. |
127 | 127 |
|
128 | | - @Code(name: "NetworkInterface", file: "NetworkInterface-03.swift") |
| 128 | + @Code(name: "NetworkInterface.swift", file: "NetworkInterface-03.swift") |
129 | 129 | } |
130 | 130 |
|
131 | 131 | @Step { |
132 | 132 | With the again-updated `NetworkInterface`, we once again have |
133 | 133 | to update `RecipeService` to work with the asynchronous |
134 | 134 | `NetworkInterface`. |
135 | 135 |
|
136 | | - @Code(name: "RecipeService", file: "RecipeService-03.swift") |
| 136 | + @Code(name: "RecipeService.swift", file: "RecipeService-03.swift") |
137 | 137 | } |
138 | 138 |
|
139 | 139 | @Step { |
|
146 | 146 | let's use the one that flattens the generics as much as |
147 | 147 | possible: `ThrowingPendableSpy`. |
148 | 148 |
|
149 | | - @Code(name: "FakeNetworkInterface", file: "FakeNetworkInterface-04.swift") |
| 149 | + @Code(name: "FakeNetworkInterface.swift", file: "FakeNetworkInterface-04.swift") |
150 | 150 | } |
151 | 151 |
|
152 | 152 | @Step { |
153 | 153 | Finally, we need to update the tests to work with all these |
154 | 154 | async calls. |
155 | 155 |
|
156 | | - @Code(name: "RecipeServiceTests", file: "RecipeServiceTests-03.swift") |
| 156 | + @Code(name: "RecipeServiceTests.swift", file: "RecipeServiceTests-03.swift") |
157 | 157 | } |
158 | 158 | } |
159 | 159 | } |
|
0 commit comments