Skip to content

Commit d154ff9

Browse files
pirjbbatsov
authored andcommitted
Relax "one expectation per example" rule
With the [introduction of `aggregate_failures` feature in RSpec 3.3](http://rspec.info/blog/2015/06/rspec-3-3-has-been-released/) there are no strong arguments left to stick to the strict style, as the output remains the same, while context initialization costs are reduced. Closes #11
1 parent 286e6a2 commit d154ff9

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,43 +281,46 @@ meant to be able to change with it.
281281

282282
## Example Structure
283283

284-
* <a name="one-expectation"></a>
285-
Use only one expectation per example. There are very few scenarios where two
286-
or more expectations in a single `it` block should be used. So, general rule
287-
of thumb is one expectation per `it` block.
288-
<sup>[[link](#one-expectation)]</sup>
284+
* <a name="one-expectation"></a><a name="expectations-per-example"></a>
285+
For examples two styles are considered acceptable. The first variant
286+
is separate example for each expectation, which comes with a cost of
287+
duplicated context initialization. The second variant is multiple
288+
expectations per example with `aggregate_failures` tag set for a
289+
group or example. Use your best judgement in each case, and apply
290+
your strategy consistently.
291+
<sup>[[link](#expectations-per-example)]</sup>
289292

290293
```ruby
291-
# bad
294+
# good - one expectation per example
292295
describe ArticlesController do
293296
#...
294297
295298
describe 'GET new' do
296-
it 'assigns new article and renders the new article template' do
299+
it 'assigns a new article' do
297300
get :new
298301
expect(assigns[:article]).to be_a(Article)
302+
end
303+
304+
it 'renders the new article template' do
305+
get :new
299306
expect(response).to render_template :new
300307
end
301308
end
302-
303-
# ...
304309
end
305310
306-
# good
311+
# good - multiple expectations with aggregated failures
307312
describe ArticlesController do
308313
#...
309314
310-
describe 'GET new' do
311-
it 'assigns a new article' do
315+
describe 'GET new', :aggregate_failures do
316+
it 'assigns new article and renders the new article template' do
312317
get :new
313318
expect(assigns[:article]).to be_a(Article)
314-
end
315-
316-
it 'renders the new article template' do
317-
get :new
318319
expect(response).to render_template :new
319320
end
320321
end
322+
323+
# ...
321324
end
322325
```
323326

0 commit comments

Comments
 (0)