O'Reilly logo

Rails 4 Test Prescriptions by Noel Rappin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Mocks to Specify Behavior

In addition to merely replacing expensive method calls, mock objects enable a different style of testing where you validate the application’s behavior rather than its ending state. In most of the tests we’ve seen throughout the book, the test validates the result of a computation: it’s testing whether something is true at the end of an action. When using mocks, however, we have the opportunity to test the process’s behavior during the test rather than the outcome.

Let’s look at an example. Back in Who Controls the Controller?, we wrote the following test of our ProjectsController:

mocks/01/gatherer/spec/controllers/projects_controller_spec.rb
 
it ​"creates a project"​ ​do
 
post :create, project: {name: ​

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required