A Note on Assertions per Test

You’ll often find that a common setup results in multiple assertions in a test. This is particularly true of integration tests. For example, when we created a new project we asserted that the project existed and had a specific relationship with newly created tasks.

There are two contrasting styles for writing tests with multiple assertions. In one style the setup and all the assertions are part of the same test. If we were trying to test changes when we mark a task complete, then having all the assertions in the same test might look like this:

​ 
it ​"marks a task complete"​ ​do​
​ 
task = tasks(:incomplete)
​ 
task.mark_complete
​ 
expect(task).to be_complete
​ 
expect(task).to be_blocked
​ ...

Get Rails 4 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.