9ec35d5ed8692a85ca928d252fdd7a2f2f3610bc
test-doubles.md
... | ... | @@ -180,6 +180,33 @@ example, you might stub a method `convert` but over time your collaborators chan |
180 | 180 | to `transform`: Your tests would continue to pass because you've stubbed a method that doesn't exist |
181 | 181 | on the real objects. |
182 | 182 | |
183 | +### The Four-Phase Test |
|
184 | + |
|
185 | +It is conventional to think of tests as having four phases: |
|
186 | + |
|
187 | +1. Setup |
|
188 | +2. Exercise |
|
189 | +3. Verify |
|
190 | +4. Teardown |
|
191 | + |
|
192 | +Typically teardown (releasing resources) is done for you by the framework. Here's our spec of `Converter` |
|
193 | +with comments showing the phases: |
|
194 | + |
|
195 | +```ruby |
|
196 | +describe Converter do |
|
197 | + let(:specific_converter) { FToCConverter.new } # setup |
|
198 | + subject { Converter.new(specific_converter) } # setup |
|
199 | + |
|
200 | + it "can delegate to a specific converter" do |
|
201 | + specific_converter.stub :convert, 400 do |
|
202 | + converted_value = subject.convert(32) # exercise |
|
203 | + expect converted_value.must_be_within_epsilon 400, 0.01 # verify |
|
204 | + end |
|
205 | + end |
|
206 | + # teardown |
|
207 | +end |
|
208 | +``` |
|
209 | + |
|
183 | 210 | ### Testing messages sent to others |
184 | 211 | |
185 | 212 | When we send a message to another object that results in a side effect, we want to verify the side effect. |