<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Class: ActionController::TestCase</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript"> // <![CDATA[ function popupCode( url ) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } function toggleCode( id ) { if ( document.getElementById ) elem = document.getElementById( id ); else if ( document.all ) elem = eval( "document.all." + id ); else return false; elemStyle = elem.style; if ( elemStyle.display != "block" ) { elemStyle.display = "block" } else { elemStyle.display = "none" } return true; } // Make codeblocks hidden by default document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) // ]]> </script> </head> <body> <div id="classHeader"> <table class="header-table"> <tr class="top-aligned-row"> <td><strong>Class</strong></td> <td class="class-name-in-header">ActionController::TestCase</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../files/lib/action_controller/test_case_rb.html"> lib/action_controller/test_case.rb </a> <br /> </td> </tr> <tr class="top-aligned-row"> <td><strong>Parent:</strong></td> <td> ActiveSupport::TestCase </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <p> Superclass for ActionController functional <a href="TestCase.html#M000234">tests</a>. Functional <a href="TestCase.html#M000234">tests</a> allow you to test a single controller action per test method. This should not be confused with integration <a href="TestCase.html#M000234">tests</a> (see <a href="IntegrationTest.html">ActionController::IntegrationTest</a>), which are more like "stories" that can involve multiple controllers and mutliple actions (i.e. multiple different HTTP requests). </p> <h2>Basic example</h2> <p> Functional <a href="TestCase.html#M000234">tests</a> are written as follows: </p> <ol> <li>First, one uses the <tt>get</tt>, <tt>post</tt>, <tt>put</tt>, <tt>delete</tt> or <tt>head</tt> method to simulate an HTTP request. </li> <li>Then, one asserts whether the current state is as expected. "State" can be anything: the controller‘s HTTP response, the database contents, etc. </li> </ol> <p> For example: </p> <pre> class BooksControllerTest < ActionController::TestCase def test_create # Simulate a POST response with the given HTTP parameters. post(:create, :book => { :title => "Love Hina" }) # Assert that the controller tried to redirect us to # the created book's URI. assert_response :found # Assert that the controller really put the book in the database. assert_not_nil Book.find_by_title("Love Hina") end end </pre> <h2>Special instance variables</h2> <p> <a href="TestCase.html">ActionController::TestCase</a> will also automatically provide the following instance variables for use in the <a href="TestCase.html#M000234">tests</a>: </p> <table> <tr><td valign="top"><b>@controller</b>:</td><td>The controller instance that will be tested. </td></tr> <tr><td valign="top"><b>@request</b>:</td><td>An ActionController::TestRequest, representing the current HTTP request. You can modify this object before sending the HTTP request. For example, you might want to set some session properties before sending a GET request. </td></tr> <tr><td valign="top"><b>@response</b>:</td><td>An <a href="TestResponse.html">ActionController::TestResponse</a> object, representing the response of the last HTTP response. In the above example, <tt>@response</tt> becomes valid after calling <tt>post</tt>. If the various assert methods are not sufficient, then you may use this object to inspect the HTTP response in detail. </td></tr> </table> <p> (Earlier versions of Rails required each functional test to subclass Test::Unit::TestCase and define @controller, @request, @response in <tt>setup</tt>.) </p> <h2>Controller is automatically inferred</h2> <p> <a href="TestCase.html">ActionController::TestCase</a> will automatically infer the controller under test from the test class name. If the controller cannot be inferred from the test class name, you can explicity set it with <tt><a href="TestCase.html#M000234">tests</a></tt>. </p> <pre> class SpecialEdgeCaseWidgetsControllerTest < ActionController::TestCase tests WidgetController end </pre> <h2>Testing controller internals</h2> <p> In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions can be used against. These collections are: </p> <ul> <li>assigns: Instance variables assigned in the action that are available for the view. </li> <li>session: Objects being saved in the session. </li> <li>flash: The flash objects currently in the session. </li> <li>cookies: <a href="Cookies.html">Cookies</a> being sent to the user on this request. </li> </ul> <p> These collections can be used just like any other hash: </p> <pre> assert_not_nil assigns(:person) # makes sure that a @person instance variable was set assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave" assert flash.empty? # makes sure that there's nothing in the flash </pre> <p> For historic reasons, the assigns hash uses string-based keys. So assigns[:person] won‘t work, but assigns["person"] will. To appease our yearning for symbols, though, an alternative accessor has been devised using a method call instead of index referencing. So assigns(:person) will work just like assigns["person"], but again, assigns[:person] will not work. </p> <p> On top of the collections, you have the complete url that a given action redirected to available in redirect_to_url. </p> <p> For redirects within the same controller, you can even call follow_redirect and the redirect will be followed, triggering another action call which can then be asserted against. </p> <h2>Manipulating the request collections</h2> <p> The collections described above link to the response, so you can test if what the actions were expected to do happened. But sometimes you also want to manipulate these collections in the incoming request. This is really only relevant for sessions and cookies, though. For sessions, you just do: </p> <pre> @request.session[:key] = "value" @request.cookies["key"] = "value" </pre> <h2>Testing named routes</h2> <p> If you‘re using named routes, they can be easily tested using the original named routes’ methods straight in the test case. Example: </p> <pre> assert_redirected_to page_url(:title => 'foo') </pre> </div> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000236">controller_class</a> <a href="#M000235">controller_class=</a> <a href="#M000237">determine_default_controller_class</a> <a href="#M000238">prepare_controller_class</a> <a href="#M000240">rescue_action_in_public!</a> <a href="#M000239">setup_controller_request_and_response</a> <a href="#M000234">tests</a> </div> </div> </div> <!-- if includes --> <div id="includes"> <h3 class="section-bar">Included Modules</h3> <div id="includes-list"> <span class="include-name"><a href="TestProcess.html">TestProcess</a></span> <span class="include-name"><a href="TestCase/Assertions.html">Assertions</a></span> </div> </div> <div id="section"> <div id="class-list"> <h3 class="section-bar">Classes and Modules</h3> Module <a href="TestCase/Assertions.html" class="link">ActionController::TestCase::Assertions</a><br /> Module <a href="TestCase/RaiseActionExceptions.html" class="link">ActionController::TestCase::RaiseActionExceptions</a><br /> </div> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Class methods</h3> <div id="method-M000236" class="method-detail"> <a name="M000236"></a> <div class="method-heading"> <a href="TestCase.src/M000236.html" target="Code" class="method-signature" onclick="popupCode('TestCase.src/M000236.html');return false;"> <span class="method-name">controller_class</span><span class="method-args">()</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000235" class="method-detail"> <a name="M000235"></a> <div class="method-heading"> <a href="TestCase.src/M000235.html" target="Code" class="method-signature" onclick="popupCode('TestCase.src/M000235.html');return false;"> <span class="method-name">controller_class=</span><span class="method-args">(new_class)</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000237" class="method-detail"> <a name="M000237"></a> <div class="method-heading"> <a href="TestCase.src/M000237.html" target="Code" class="method-signature" onclick="popupCode('TestCase.src/M000237.html');return false;"> <span class="method-name">determine_default_controller_class</span><span class="method-args">(name)</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000238" class="method-detail"> <a name="M000238"></a> <div class="method-heading"> <a href="TestCase.src/M000238.html" target="Code" class="method-signature" onclick="popupCode('TestCase.src/M000238.html');return false;"> <span class="method-name">prepare_controller_class</span><span class="method-args">(new_class)</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000234" class="method-detail"> <a name="M000234"></a> <div class="method-heading"> <a href="TestCase.src/M000234.html" target="Code" class="method-signature" onclick="popupCode('TestCase.src/M000234.html');return false;"> <span class="method-name">tests</span><span class="method-args">(controller_class)</span> </a> </div> <div class="method-description"> <p> Sets the controller class name. Useful if the name can‘t be inferred from test class. Expects <tt><a href="TestCase.html#M000236">controller_class</a></tt> as a constant. Example: <tt><a href="TestCase.html#M000234">tests</a> WidgetController</tt>. </p> </div> </div> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000240" class="method-detail"> <a name="M000240"></a> <div class="method-heading"> <a href="TestCase.src/M000240.html" target="Code" class="method-signature" onclick="popupCode('TestCase.src/M000240.html');return false;"> <span class="method-name">rescue_action_in_public!</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Cause the action to be rescued according to the regular rules for rescue_action when the visitor is not local </p> </div> </div> <div id="method-M000239" class="method-detail"> <a name="M000239"></a> <div class="method-heading"> <a href="TestCase.src/M000239.html" target="Code" class="method-signature" onclick="popupCode('TestCase.src/M000239.html');return false;"> <span class="method-name">setup_controller_request_and_response</span><span class="method-args">()</span> </a> </div> <div class="method-description"> </div> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html>