0a744e499629c3505548f0728337209f64bffa91
test-doubles.md
... | ... | @@ -140,8 +140,45 @@ of the delegation to the specific converter even if it's wrong; our stubbed test |
140 | 140 | what the actual class does at all: our test is completely independent. Additionally, we have moved the |
141 | 141 | key verification value closer to the assertion, which is easier to read. |
142 | 142 | |
143 | +For here on out, we will use a different syntax in MiniTest. Here's the same test but using |
|
144 | +spec-style syntax using keywords such as "describe" and "expect." |
|
145 | + |
|
146 | +```ruby |
|
147 | +require 'minitest/autorun' |
|
148 | + |
|
149 | +class FToCConverter |
|
150 | + def convert(value) |
|
151 | + 500 |
|
152 | + end |
|
153 | +end |
|
154 | + |
|
155 | +class Converter |
|
156 | + def initialize(specific_converter) |
|
157 | + @specific_converter = specific_converter |
|
158 | + end |
|
159 | + def convert(value) |
|
160 | + @specific_converter.convert(value) |
|
161 | + end |
|
162 | +end |
|
163 | + |
|
164 | +describe Converter do |
|
165 | + let(:specific_converter) { FToCConverter.new } |
|
166 | + subject { Converter.new(specific_converter) } |
|
167 | + |
|
168 | + it "can delegate to a specific converter" do |
|
169 | + specific_converter.stub :convert, 400 do |
|
170 | + expect subject.convert(32).must_be_within_epsilon 400, 0.01 |
|
171 | + end |
|
172 | + end |
|
173 | +end |
|
174 | +``` |
|
175 | + |
|
143 | 176 | Stubbing in MiniTest requires the the stubbed method actually exist in the instance being stubbed. Other |
144 | -testing frameworks are more lenient in this respect, which can result in more concise tests. |
|
177 | +testing frameworks are more lenient in this respect, which can result in more concise tests -- but at |
|
178 | +the expense of having test doubles that are _too_ fake and don't match up to your real collaborators. For |
|
179 | +example, you might stub a method `convert` but over time your collaborators change that method name |
|
180 | +to `transform`: Your tests would continue to pass because you've stubbed a method that doesn't exist |
|
181 | +on the real objects. |
|
145 | 182 | |
146 | 183 | ### Testing messages sent to others |
147 | 184 | |
... | ... | @@ -161,4 +198,4 @@ When we send a message to another object that results in a side effect, we want |
161 | 198 | Other topics: |
162 | 199 | |
163 | 200 | * The four-phase test. |
164 | -* Fixtures. |
|
201 | +* Fixtures. |
|
... | ... | \ No newline at end of file |