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

Simulating Requests in a Controller Test

Most of your controller tests in Rails will surround a simulated request. To make this simulation easier, Rails provides a controller test method for each HTTP verb: delete, get, head, patch, post, and put. Each of these methods works the same way. (Internally, they all dispatch to a common method that does all the work.) A full call to one of these methods has five arguments, though you’ll often just use the first three:

 
get :show, {id: @task.id}, {user_id: ​"3"​,
 
current_project: @project.id.to_s}, {notice: ​"flash test"​}

The method name, get, is the HTTP verb being simulated—sort of. While the controller test will set the HTTP verb if for some reason you query the Rails request object, it does ...

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