aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/html/testing_rails_applications.html
diff options
context:
space:
mode:
authorMike Gunderloy <MikeG1@larkfarm.com>2008-12-17 07:10:53 -0600
committerMike Gunderloy <MikeG1@larkfarm.com>2008-12-17 07:11:59 -0600
commit17f3ab3b83229fa731a5dbb864d385414633cf99 (patch)
tree80a12e72f8a35fbb4c0c7be6637fbfb09ef43d86 /railties/doc/guides/html/testing_rails_applications.html
parent9b2dd332f12902c520f7148f6380d313252613d8 (diff)
downloadrails-17f3ab3b83229fa731a5dbb864d385414633cf99.tar.gz
rails-17f3ab3b83229fa731a5dbb864d385414633cf99.tar.bz2
rails-17f3ab3b83229fa731a5dbb864d385414633cf99.zip
Fix bad link in Getting Started, rebuild HTML
Diffstat (limited to 'railties/doc/guides/html/testing_rails_applications.html')
-rw-r--r--railties/doc/guides/html/testing_rails_applications.html1470
1 files changed, 516 insertions, 954 deletions
diff --git a/railties/doc/guides/html/testing_rails_applications.html b/railties/doc/guides/html/testing_rails_applications.html
index b8a99767ee..a94c81dc5f 100644
--- a/railties/doc/guides/html/testing_rails_applications.html
+++ b/railties/doc/guides/html/testing_rails_applications.html
@@ -219,48 +219,9 @@ ul#navMain {
<li><a href="#_preparing_you_application_for_testing">Preparing you Application for Testing</a></li>
- <li><a href="#_running_tests">Running Tests</a></li>
-
- <li><a href="#_what_to_include_in_your_unit_tests">What to Include in Your Unit Tests</a></li>
-
- <li><a href="#_assertions_available">Assertions Available</a></li>
-
- <li><a href="#_rails_specific_assertions">Rails Specific Assertions</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_functional_tests_for_your_controllers">Functional Tests for Your Controllers</a>
- <ul>
-
- <li><a href="#_what_to_include_in_your_functional_tests">What to include in your Functional Tests</a></li>
-
- <li><a href="#_available_request_types_for_functional_tests">Available Request Types for Functional Tests</a></li>
-
- <li><a href="#_the_4_hashes_of_the_apocalypse">The 4 Hashes of the Apocalypse</a></li>
-
- <li><a href="#_instance_variables_available">Instance Variables Available</a></li>
-
- <li><a href="#_a_fuller_functional_test_example">A Fuller Functional Test Example</a></li>
-
- <li><a href="#_testing_views">Testing Views</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_integration_testing">Integration Testing</a>
- <ul>
-
- <li><a href="#_helpers_available_for_integration_tests">Helpers Available for Integration tests</a></li>
-
- <li><a href="#_integration_testing_examples">Integration Testing Examples</a></li>
-
</ul>
</li>
<li>
- <a href="#_rake_tasks_for_running_your_tests">Rake Tasks for Running your Tests</a>
- </li>
- <li>
<a href="#_brief_note_about_test_unit">Brief Note About Test::Unit</a>
</li>
<li>
@@ -585,994 +546,595 @@ $ rake db<span style="color: #990000">:</span><span style="font-weight: bold"><s
<td class="content"><tt>db:test:prepare</tt> will fail with an error if db/schema.rb doesn't exists.</td>
</tr></table>
</div>
-<h4 id="_rake_tasks_for_preparing_you_application_for_testing">3.1.1. Rake Tasks for Preparing you Application for Testing ==</h4>
-<div class="para"><p>--------------------------------`----------------------------------------------------
-Tasks Description</p></div>
+<h4 id="_rake_tasks_for_preparing_your_application_for_testing">3.1.1. Rake Tasks for Preparing your Application for Testing</h4>
<div class="listingblock">
<div class="content">
-<pre><tt>+rake db:test:clone+ Recreate the test database from the current environment's database schema
-+rake db:test:clone_structure+ Recreate the test databases from the development structure
-+rake db:test:load+ Recreate the test database from the current +schema.rb+
-+rake db:test:prepare+ Check for pending migrations and load the test schema
-+rake db:test:purge+ Empty the test database.</tt></pre>
+<pre><tt>Tasks Description</tt></pre>
</div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">You can see all these rake tasks and their descriptions by running <tt>rake &#8212;tasks &#8212;describe</tt></td>
-</tr></table>
-</div>
-<h3 id="_running_tests">3.2. Running Tests</h3>
-<div class="para"><p>Running a test is as simple as invoking the file containing the test cases through Ruby:</p></div>
+<div class="para"><p><tt>rake db:test:clone</tt> Recreate the test database from the current environment's database schema
+<tt>rake db:test:clone_structure</tt> Recreate the test databases from the development structure
+<tt>rake db:test:load</tt> Recreate the test database from the current <tt>schema.rb</tt>
+<tt>rake db:test:prepare</tt> Check for pending migrations and load the test schema
+<tt>rake db:test:purge</tt> Empty the test database.</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ cd <span style="font-weight: bold"><span style="color: #0000FF">test</span></span>
-$ ruby unit/post_test<span style="color: #990000">.</span>rb
+<div class="content">
+<pre><tt>
+TIP: You can see all these rake tasks and their descriptions by running +rake \-\-tasks \-\-describe+
-Loaded suite unit/post_test
-Started
-<span style="color: #990000">.</span>
-Finished <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> <span style="color: #993399">0.023513</span> seconds<span style="color: #990000">.</span>
+=== Running Tests ===
-<span style="color: #993399">1</span> tests<span style="color: #990000">,</span> <span style="color: #993399">1</span> assertions<span style="color: #990000">,</span> <span style="color: #993399">0</span> failures<span style="color: #990000">,</span> <span style="color: #993399">0</span> errors
-</tt></pre></div></div>
-<div class="para"><p>This will run all the test methods from the test case.</p></div>
-<div class="para"><p>You can also run a particular test method from the test case by using the <tt>-n</tt> switch with the <tt>test method name</tt>.</p></div>
+Running a test is as simple as invoking the file containing the test cases through Ruby:
+
+[source, shell]</tt></pre>
+</div></div>
+<div class="para"><p>$ cd test
+$ ruby unit/post_test.rb</p></div>
+<div class="para"><p>Loaded suite unit/post_test
+Started
+.
+Finished in 0.023513 seconds.</p></div>
+<div class="para"><p>1 tests, 1 assertions, 0 failures, 0 errors</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>$ ruby unit/post_test.rb -n test_truth
+<pre><tt>
+This will run all the test methods from the test case.
-Loaded suite unit/post_test
+You can also run a particular test method from the test case by using the +-n+ switch with the +test method name+.</tt></pre>
+</div></div>
+<div class="para"><p>$ ruby unit/post_test.rb -n test_truth</p></div>
+<div class="para"><p>Loaded suite unit/post_test
Started
.
-Finished in 0.023513 seconds.
+Finished in 0.023513 seconds.</p></div>
+<div class="para"><p>1 tests, 1 assertions, 0 failures, 0 errors</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>
+The +.+ (dot) above indicates a passing test. When a test fails you see an +F+; when a test throws an error you see an +E+ in its place. The last line of the output is the summary.
-1 tests, 1 assertions, 0 failures, 0 errors</tt></pre>
+To see how a test failure is reported, you can add a failing test to the +post_test.rb+ test case.
+
+[source,ruby]</tt></pre>
</div></div>
-<div class="para"><p>The <tt>.</tt> (dot) above indicates a passing test. When a test fails you see an <tt>F</tt>; when a test throws an error you see an <tt>E</tt> in its place. The last line of the output is the summary.</p></div>
-<div class="para"><p>To see how a test failure is reported, you can add a failing test to the <tt>post_test.rb</tt> test case.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_not_save_post_without_title
- post <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new
- assert <span style="color: #990000">!</span>post<span style="color: #990000">.</span>save
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Let us run this newly added test.</p></div>
+<div class="para"><p>def test_should_not_save_post_without_title
+ post = Post.new
+ assert !post.save
+end</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
+<pre><tt>
+Let us run this newly added test.</tt></pre>
+</div></div>
+<div class="para"><p>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
Loaded suite unit/post_test
Started
F
-Finished in 0.197094 seconds.
-
- 1) Failure:
+Finished in 0.197094 seconds.</p></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt> 1) Failure:
test_should_not_save_post_without_title(PostTest)
[unit/post_test.rb:11:in `test_should_not_save_post_without_title'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
-&lt;false&gt; is not true.
-
-1 tests, 1 assertions, 1 failures, 0 errors</tt></pre>
+&lt;false&gt; is not true.</tt></pre>
</div></div>
-<div class="para"><p>In the output, <tt>F</tt> denotes a failure. You can see the corresponding trace shown under <tt>1)</tt> along with the name of the failing test. The next few lines contain the stack trace followed by a message which mentions the actual value and the expected value by the assertion. The default assertion messages provide just enough information to help pinpoint the error. To make the assertion failure message more readable every assertion provides an optional message parameter, as shown here:</p></div>
+<div class="para"><p>1 tests, 1 assertions, 1 failures, 0 errors</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_not_save_post_without_title
- post <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new
- assert <span style="color: #990000">!</span>post<span style="color: #990000">.</span>save<span style="color: #990000">,</span> <span style="color: #FF0000">"Saved the post without a title"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Running this test shows the friendlier assertion message:</p></div>
+<div class="content">
+<pre><tt>
+In the output, +F+ denotes a failure. You can see the corresponding trace shown under +1)+ along with the name of the failing test. The next few lines contain the stack trace followed by a message which mentions the actual value and the expected value by the assertion. The default assertion messages provide just enough information to help pinpoint the error. To make the assertion failure message more readable every assertion provides an optional message parameter, as shown here:
+
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>def test_should_not_save_post_without_title
+ post = Post.new
+ assert !post.save, "Saved the post without a title"
+end</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
+<pre><tt>
+Running this test shows the friendlier assertion message:</tt></pre>
+</div></div>
+<div class="para"><p>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
Loaded suite unit/post_test
Started
F
-Finished in 0.198093 seconds.
-
- 1) Failure:
+Finished in 0.198093 seconds.</p></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt> 1) Failure:
test_should_not_save_post_without_title(PostTest)
[unit/post_test.rb:11:in `test_should_not_save_post_without_title'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
Saved the post without a title.
-&lt;false&gt; is not true.
-
-1 tests, 1 assertions, 1 failures, 0 errors</tt></pre>
+&lt;false&gt; is not true.</tt></pre>
</div></div>
-<div class="para"><p>Now to get this test to pass we can add a model level validation for the <em>title</em> field.</p></div>
+<div class="para"><p>1 tests, 1 assertions, 1 failures, 0 errors</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>title
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Now the test should pass. Let us verify by running the test again:</p></div>
+<div class="content">
+<pre><tt>
+Now to get this test to pass we can add a model level validation for the _title_ field.
+
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>class Post &lt; ActiveRecord::Base
+ validates_presence_of :title
+end</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
+<pre><tt>
+Now the test should pass. Let us verify by running the test again:</tt></pre>
+</div></div>
+<div class="para"><p>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
Loaded suite unit/post_test
Started
.
-Finished in 0.193608 seconds.
+Finished in 0.193608 seconds.</p></div>
+<div class="para"><p>1 tests, 1 assertions, 0 failures, 0 errors</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>
+Now if you noticed we first wrote a test which fails for a desired functionality, then we wrote some code which adds the functionality and finally we ensured that our test passes. This approach to software development is referred to as _Test-Driven Development_ (TDD).
-1 tests, 1 assertions, 0 failures, 0 errors</tt></pre>
+TIP: Many Rails developers practice _Test-Driven Development_ (TDD). This is an excellent way to build up a test suite that exercises every part of your application. TDD is beyond the scope of this guide, but one place to start is with link:http://andrzejonsoftware.blogspot.com/2007/05/15-tdd-steps-to-create-rails.html[15 TDD steps to create a Rails application].
+
+To see how an error gets reported, here's a test containing an error:
+
+[source,ruby]</tt></pre>
</div></div>
-<div class="para"><p>Now if you noticed we first wrote a test which fails for a desired functionality, then we wrote some code which adds the functionality and finally we ensured that our test passes. This approach to software development is referred to as <em>Test-Driven Development</em> (TDD).</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">Many Rails developers practice <em>Test-Driven Development</em> (TDD). This is an excellent way to build up a test suite that exercises every part of your application. TDD is beyond the scope of this guide, but one place to start is with <a href="http://andrzejonsoftware.blogspot.com/2007/05/15-tdd-steps-to-create-rails.html">15 TDD steps to create a Rails application</a>.</td>
-</tr></table>
-</div>
-<div class="para"><p>To see how an error gets reported, here's a test containing an error:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_report_error
- <span style="font-style: italic"><span style="color: #9A1900"># some_undefined_variable is not defined elsewhere in the test case</span></span>
+<div class="para"><p>def test_should_report_error
+ # some_undefined_variable is not defined elsewhere in the test case
some_undefined_variable
- assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Now you can see even more output in the console from running the tests:</p></div>
+ assert true
+end</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>$ ruby unit/post_test.rb -n test_should_report_error
+<pre><tt>
+Now you can see even more output in the console from running the tests:</tt></pre>
+</div></div>
+<div class="para"><p>$ ruby unit/post_test.rb -n test_should_report_error
Loaded suite unit/post_test
Started
E
-Finished in 0.195757 seconds.
-
- 1) Error:
+Finished in 0.195757 seconds.</p></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt> 1) Error:
test_should_report_error(PostTest):
NameError: undefined local variable or method `some_undefined_variable' for #&lt;PostTest:0x2cc9de8&gt;
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/test_process.rb:467:in `method_missing'
unit/post_test.rb:16:in `test_should_report_error'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
- /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run'
-
-1 tests, 0 assertions, 0 failures, 1 errors</tt></pre>
+ /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run'</tt></pre>
</div></div>
-<div class="para"><p>Notice the <em>E</em> in the output. It denotes a test with error.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The execution of each test method stops as soon as any error or a assertion failure is encountered, and the test suite continues with the next method. All test methods are executed in alphabetical order.</td>
-</tr></table>
-</div>
-<h3 id="_what_to_include_in_your_unit_tests">3.3. What to Include in Your Unit Tests</h3>
-<div class="para"><p>Ideally you would like to include a test for everything which could possibly break. It's a good practice to have at least one test for each of your validations and at least one test for every method in your model.</p></div>
-<h3 id="_assertions_available">3.4. Assertions Available</h3>
-<div class="para"><p>By now you've caught a glimpse of some of the assertions that are available. Assertions are the worker bees of testing. They are the ones that actually perform the checks to ensure that things are going as planned.</p></div>
-<div class="para"><p>There are a bunch of different types of assertions you can use. Here's the complete list of assertions that ship with <tt>test/unit</tt>, the testing library used by Rails. The <tt>[msg]</tt> parameter is an optional string message you can specify to make your test failure messages clearer. It's not required.</p></div>
-<div class="tableblock">
-<table rules="all"
-frame="hsides"
-cellspacing="0" cellpadding="4">
-<col width="754" />
-<col width="834" />
-<thead>
- <tr>
- <th align="left">
- Assertion
- </th>
- <th align="left">
- Purpose
- </th>
- </tr>
-</thead>
-<tbody valign="top">
- <tr>
- <td align="left">
- <tt>assert( boolean, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that the object/expression is true.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_equal( obj1, obj2, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that <tt>obj1 == obj2</tt> is true.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_not_equal( obj1, obj2, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that <tt>obj1 == obj2</tt> is false.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_same( obj1, obj2, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that <tt>obj1.equal?(obj2)</tt> is true.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_not_same( obj1, obj2, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that <tt>obj1.equal?(obj2)</tt> is false.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_nil( obj, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that <tt>obj.nil?</tt> is true.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_not_nil( obj, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that <tt>obj.nil?</tt> is false.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_match( regexp, string, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that a string matches the regular expression.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_no_match( regexp, string, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that a string doesn't matches the regular expression.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_in_delta( expecting, actual, delta, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that the numbers <tt>expecting</tt> and <tt>actual</tt> are within <tt>delta</tt> of each other.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_throws( symbol, [msg] ) { block }</tt>
- </td>
- <td align="left">
- Ensures that the given block throws the symbol.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_raises( exception1, exception2, &#8230; ) { block }</tt>
- </td>
- <td align="left">
- Ensures that the given block raises one of the given exceptions.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_nothing_raised( exception1, exception2, &#8230; ) { block }</tt>
- </td>
- <td align="left">
- Ensures that the given block doesn't raise one of the given exceptions.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_instance_of( class, obj, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that <tt>obj</tt> is of the <tt>class</tt> type.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_kind_of( class, obj, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that <tt>obj</tt> is or descends from <tt>class</tt>.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_respond_to( obj, symbol, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that <tt>obj</tt> has a method called <tt>symbol</tt>.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_operator( obj1, operator, obj2, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that <tt>obj1.operator(obj2)</tt> is true.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_send( array, [msg] )</tt>
- </td>
- <td align="left">
- Ensures that executing the method listed in <tt>array[1]</tt> on the object in <tt>array[0]</tt> with the parameters of <tt>array[2 and up]</tt> is true. This one is weird eh?
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>flunk( [msg] )</tt>
- </td>
- <td align="left">
- Ensures failure. This is useful to explicitly mark a test that isn't finished yet.
- </td>
- </tr>
-</tbody>
-</table>
-</div>
-<div class="para"><p>Because of the modular nature of the testing framework, it is possible to create your own assertions. In fact, that's exactly what Rails does. It includes some specialized assertions to make your life easier.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Creating your own assertions is an advanced topic that we won't cover in this tutorial.</td>
-</tr></table>
-</div>
-<h3 id="_rails_specific_assertions">3.5. Rails Specific Assertions</h3>
-<div class="para"><p>Rails adds some custom assertions of its own to the <tt>test/unit</tt> framework:</p></div>
-<div class="tableblock">
-<table rules="all"
-frame="hsides"
-cellspacing="0" cellpadding="4">
-<col width="948" />
-<col width="640" />
-<thead>
- <tr>
- <th align="left">
- Assertion
- </th>
- <th align="left">
- Purpose
- </th>
- </tr>
-</thead>
-<tbody valign="top">
- <tr>
- <td align="left">
- <tt>assert_valid(record)</tt>
- </td>
- <td align="left">
- Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_difference(expressions, difference = 1, message = nil) {|| &#8230;}</tt>
- </td>
- <td align="left">
- Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_no_difference(expressions, message = nil, &amp;block)</tt>
- </td>
- <td align="left">
- Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_recognizes(expected_options, path, extras={}, message=nil)</tt>
- </td>
- <td align="left">
- Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)</tt>
- </td>
- <td align="left">
- Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_response(type, message = nil)</tt>
- </td>
- <td align="left">
- Asserts that the response comes with a specific status code. You can specify <tt>:success</tt> to indicate 200, <tt>:redirect</tt> to indicate 300-399, <tt>:missing</tt> to indicate 404, or <tt>:error</tt> to match the 500-599 range
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_redirected_to(options = {}, message=nil)</tt>
- </td>
- <td align="left">
- Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that <tt>assert_redirected_to(:controller &#8658; "weblog")</tt> will also match the redirection of <tt>redirect_to(:controller &#8658; "weblog", :action &#8658; "show")</tt> and so on.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_template(expected = nil, message=nil)</tt>
- </td>
- <td align="left">
- Asserts that the request was rendered with the appropriate template file.
- </td>
- </tr>
-</tbody>
-</table>
-</div>
-<div class="para"><p>You'll see the usage of some of these assertions in the next chapter.</p></div>
-</div>
-<h2 id="_functional_tests_for_your_controllers">4. Functional Tests for Your Controllers</h2>
-<div class="sectionbody">
-<div class="para"><p>In Rails, testing the various actions of a single controller is called writing functional tests for that controller. Controllers handle the incoming web requests to your application and eventually respond with a rendered view.</p></div>
-<h3 id="_what_to_include_in_your_functional_tests">4.1. What to include in your Functional Tests</h3>
-<div class="para"><p>You should test for things such as:</p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-was the web request successful?
-</p>
-</li>
-<li>
-<p>
-was the user redirected to the right page?
-</p>
-</li>
-<li>
-<p>
-was the user successfully authenticated?
-</p>
-</li>
-<li>
-<p>
-was the correct object stored in the response template?
-</p>
-</li>
-<li>
-<p>
-was the appropriate message displayed to the user in the view
-</p>
-</li>
-</ul></div>
-<div class="para"><p>Now that we have used Rails scaffold generator for our <tt>Post</tt> resource, it has already created the controller code and functional tests. You can take look at the file <tt>posts_controller_test.rb</tt> in the <tt>test/functional</tt> directory.</p></div>
-<div class="para"><p>Let me take you through one such test, <tt>test_should_get_index</tt> from the file <tt>posts_controller_test.rb</tt>.</p></div>
+<div class="para"><p>1 tests, 0 assertions, 0 failures, 1 errors</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_get_index
- get <span style="color: #990000">:</span>index
- assert_response <span style="color: #990000">:</span>success
- assert_not_nil assigns<span style="color: #990000">(:</span>posts<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In the <tt>test_should_get_index</tt> test, Rails simulates a request on the action called index, making sure the request was successful and also ensuring that it assigns a valid <tt>posts</tt> instance variable.</p></div>
-<div class="para"><p>The <tt>get</tt> method kicks off the web request and populates the results into the response. It accepts 4 arguments:</p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-The action of the controller you are requesting. This can be in the form of a string or a symbol.
-</p>
-</li>
-<li>
-<p>
-An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).
-</p>
-</li>
-<li>
-<p>
-An optional hash of session variables to pass along with the request.
-</p>
-</li>
-<li>
-<p>
-An optional hash of flash values.
-</p>
-</li>
-</ul></div>
-<div class="para"><p>Example: Calling the <tt>:show</tt> action, passing an <tt>id</tt> of 12 as the <tt>params</tt> and setting a <tt>user_id</tt> of 5 in the session:</p></div>
+<div class="content">
+<pre><tt>
+Notice the 'E' in the output. It denotes a test with error.
+
+NOTE: The execution of each test method stops as soon as any error or a assertion failure is encountered, and the test suite continues with the next method. All test methods are executed in alphabetical order.
+
+=== What to Include in Your Unit Tests ===
+
+Ideally you would like to include a test for everything which could possibly break. It's a good practice to have at least one test for each of your validations and at least one test for every method in your model.
+
+=== Assertions Available ===
+
+By now you've caught a glimpse of some of the assertions that are available. Assertions are the worker bees of testing. They are the ones that actually perform the checks to ensure that things are going as planned.
+
+There are a bunch of different types of assertions you can use. Here's the complete list of assertions that ship with +test/unit+, the testing library used by Rails. The +[msg]+ parameter is an optional string message you can specify to make your test failure messages clearer. It's not required.
+
+[grid="all"]
+`-----------------------------------------------------------------`------------------------------------------------------------------------
+Assertion Purpose</tt></pre>
+</div></div>
+<div class="para"><p><tt>assert( boolean, [msg] )</tt> Ensures that the object/expression is true.
+<tt>assert_equal( obj1, obj2, [msg] )</tt> Ensures that <tt>obj1 == obj2</tt> is true.
+<tt>assert_not_equal( obj1, obj2, [msg] )</tt> Ensures that <tt>obj1 == obj2</tt> is false.
+<tt>assert_same( obj1, obj2, [msg] )</tt> Ensures that <tt>obj1.equal?(obj2)</tt> is true.
+<tt>assert_not_same( obj1, obj2, [msg] )</tt> Ensures that <tt>obj1.equal?(obj2)</tt> is false.
+<tt>assert_nil( obj, [msg] )</tt> Ensures that <tt>obj.nil?</tt> is true.
+<tt>assert_not_nil( obj, [msg] )</tt> Ensures that <tt>obj.nil?</tt> is false.
+<tt>assert_match( regexp, string, [msg] )</tt> Ensures that a string matches the regular expression.
+<tt>assert_no_match( regexp, string, [msg] )</tt> Ensures that a string doesn't matches the regular expression.
+<tt>assert_in_delta( expecting, actual, delta, [msg] )</tt> Ensures that the numbers <tt>expecting</tt> and <tt>actual</tt> are within <tt>delta</tt> of each other.
+<tt>assert_throws( symbol, [msg] ) { block }</tt> Ensures that the given block throws the symbol.
+<tt>assert_raises( exception1, exception2, &#8230; ) { block }</tt> Ensures that the given block raises one of the given exceptions.
+<tt>assert_nothing_raised( exception1, exception2, &#8230; ) { block }</tt> Ensures that the given block doesn't raise one of the given exceptions.
+<tt>assert_instance_of( class, obj, [msg] )</tt> Ensures that <tt>obj</tt> is of the <tt>class</tt> type.
+<tt>assert_kind_of( class, obj, [msg] )</tt> Ensures that <tt>obj</tt> is or descends from <tt>class</tt>.
+<tt>assert_respond_to( obj, symbol, [msg] )</tt> Ensures that <tt>obj</tt> has a method called <tt>symbol</tt>.
+<tt>assert_operator( obj1, operator, obj2, [msg] )</tt> Ensures that <tt>obj1.operator(obj2)</tt> is true.
+<tt>assert_send( array, [msg] )</tt> Ensures that executing the method listed in <tt>array[1]</tt> on the object in <tt>array[0]</tt> with the parameters of <tt>array[2 and up]</tt> is true. This one is weird eh?
+<tt>flunk( [msg] )</tt> Ensures failure. This is useful to explicitly mark a test that isn't finished yet.</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>get<span style="color: #990000">(:</span>show<span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'id'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"12"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'user_id'</span> <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>Another example: Calling the <tt>:view</tt> action, passing an <tt>id</tt> of 12 as the <tt>params</tt>, this time with no session, but with a flash message.</p></div>
+<div class="content">
+<pre><tt>
+Because of the modular nature of the testing framework, it is possible to create your own assertions. In fact, that's exactly what Rails does. It includes some specialized assertions to make your life easier.
+
+NOTE: Creating your own assertions is an advanced topic that we won't cover in this tutorial.
+
+=== Rails Specific Assertions ===
+
+Rails adds some custom assertions of its own to the +test/unit+ framework:
+
+[grid="all"]
+`----------------------------------------------------------------------------------`-------------------------------------------------------
+Assertion Purpose</tt></pre>
+</div></div>
+<div class="para"><p><tt>assert_valid(record)</tt> Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.
+<tt>assert_difference(expressions, difference = 1, message = nil) {|| &#8230;}</tt> Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.
+<tt>assert_no_difference(expressions, message = nil, &amp;block)</tt> Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.
+<tt>assert_recognizes(expected_options, path, extras={}, message=nil)</tt> Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.
+<tt>assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)</tt> Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.
+<tt>assert_response(type, message = nil)</tt> Asserts that the response comes with a specific status code. You can specify <tt>:success</tt> to indicate 200, <tt>:redirect</tt> to indicate 300-399, <tt>:missing</tt> to indicate 404, or <tt>:error</tt> to match the 500-599 range
+<tt>assert_redirected_to(options = {}, message=nil)</tt> Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that <tt>assert_redirected_to(:controller &#8658; "weblog")</tt> will also match the redirection of <tt>redirect_to(:controller &#8658; "weblog", :action &#8658; "show")</tt> and so on.
+<tt>assert_template(expected = nil, message=nil)</tt> Asserts that the request was rendered with the appropriate template file.</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>get<span style="color: #990000">(:</span>view<span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'id'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'12'</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'message'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'booya!'</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">If you try running <tt>test_should_create_post</tt> test from <tt>posts_controller_test.rb</tt> it will fail on account of the newly added model level validation and rightly so.</td>
-</tr></table>
-</div>
-<div class="para"><p>Let us modify <tt>test_should_create_post</tt> test in <tt>posts_controller_test.rb</tt> so that all our test pass:</p></div>
+<div class="content">
+<pre><tt>
+You'll see the usage of some of these assertions in the next chapter.
+
+== Functional Tests for Your Controllers ==
+
+In Rails, testing the various actions of a single controller is called writing functional tests for that controller. Controllers handle the incoming web requests to your application and eventually respond with a rendered view.
+
+=== What to include in your Functional Tests ===
+
+You should test for things such as:
+
+ * was the web request successful?
+ * was the user redirected to the right page?
+ * was the user successfully authenticated?
+ * was the correct object stored in the response template?
+ * was the appropriate message displayed to the user in the view
+
+Now that we have used Rails scaffold generator for our +Post+ resource, it has already created the controller code and functional tests. You can take look at the file +posts_controller_test.rb+ in the +test/functional+ directory.
+
+Let me take you through one such test, +test_should_get_index+ from the file +posts_controller_test.rb+.
+
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>def test_should_get_index
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:posts)
+end</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_create_post
- assert_difference<span style="color: #990000">(</span><span style="color: #FF0000">'Post.count'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- post <span style="color: #990000">:</span>create<span style="color: #990000">,</span> <span style="color: #990000">:</span>post <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>title <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Some title'</span><span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<div class="content">
+<pre><tt>
+In the +test_should_get_index+ test, Rails simulates a request on the action called index, making sure the request was successful and also ensuring that it assigns a valid +posts+ instance variable.
- assert_redirected_to post_path<span style="color: #990000">(</span>assigns<span style="color: #990000">(:</span>post<span style="color: #990000">))</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Now you can try running all the tests and they should pass.</p></div>
-<h3 id="_available_request_types_for_functional_tests">4.2. Available Request Types for Functional Tests</h3>
-<div class="para"><p>If you're familiar with the HTTP protocol, you'll know that <tt>get</tt> is a type of request. There are 5 request types supported in Rails functional tests:</p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-<tt>get</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>post</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>put</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>head</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>delete</tt>
-</p>
-</li>
-</ul></div>
-<div class="para"><p>All of request types are methods that you can use, however, you'll probably end up using the first two more often than the others.</p></div>
-<h3 id="_the_4_hashes_of_the_apocalypse">4.3. The 4 Hashes of the Apocalypse</h3>
-<div class="para"><p>After a request has been made by using one of the 5 methods (<tt>get</tt>, <tt>post</tt>, etc.) and processed, you will have 4 Hash objects ready for use:</p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-<tt>assigns</tt> - Any objects that are stored as instance variables in actions for use in views.
-</p>
-</li>
-<li>
-<p>
-<tt>cookies</tt> - Any cookies that are set.
-</p>
-</li>
-<li>
-<p>
-<tt>flash</tt> - Any objects living in the flash.
-</p>
-</li>
-<li>
-<p>
-<tt>session</tt> - Any object living in session variables.
-</p>
-</li>
-</ul></div>
-<div class="para"><p>As is the case with normal Hash objects, you can access the values by referencing the keys by string. You can also reference them by symbol name, except for <tt>assigns</tt>. For example:</p></div>
+The +get+ method kicks off the web request and populates the results into the response. It accepts 4 arguments:
+
+* The action of the controller you are requesting. This can be in the form of a string or a symbol.
+* An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).
+* An optional hash of session variables to pass along with the request.
+* An optional hash of flash values.
+
+Example: Calling the +:show+ action, passing an +id+ of 12 as the +params+ and setting a +user_id+ of 5 in the session:
+
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>get(:show, {<em>id</em> &#8658; "12"}, {<em>user_id</em> &#8658; 5})</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt> flash<span style="color: #990000">[</span><span style="color: #FF0000">"gordon"</span><span style="color: #990000">]</span> flash<span style="color: #990000">[:</span>gordon<span style="color: #990000">]</span>
- session<span style="color: #990000">[</span><span style="color: #FF0000">"shmession"</span><span style="color: #990000">]</span> session<span style="color: #990000">[:</span>shmession<span style="color: #990000">]</span>
- cookies<span style="color: #990000">[</span><span style="color: #FF0000">"are_good_for_u"</span><span style="color: #990000">]</span> cookies<span style="color: #990000">[:</span>are_good_for_u<span style="color: #990000">]</span>
+<div class="content">
+<pre><tt>
+Another example: Calling the +:view+ action, passing an +id+ of 12 as the +params+, this time with no session, but with a flash message.
-<span style="font-style: italic"><span style="color: #9A1900"># Because you can't use assigns[:something] for historical reasons:</span></span>
- assigns<span style="color: #990000">[</span><span style="color: #FF0000">"something"</span><span style="color: #990000">]</span> assigns<span style="color: #990000">(:</span>something<span style="color: #990000">)</span>
-</tt></pre></div></div>
-<h3 id="_instance_variables_available">4.4. Instance Variables Available</h3>
-<div class="para"><p>You also have access to three instance variables in your functional tests:</p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-<tt>@controller</tt> - The controller processing the request
-</p>
-</li>
-<li>
-<p>
-<tt>@request</tt> - The request
-</p>
-</li>
-<li>
-<p>
-<tt>@response</tt> - The response
-</p>
-</li>
-</ul></div>
-<h3 id="_a_fuller_functional_test_example">4.5. A Fuller Functional Test Example</h3>
-<div class="para"><p>Here's another example that uses <tt>flash</tt>, <tt>assert_redirected_to</tt>, and <tt>assert_difference</tt>:</p></div>
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>get(:view, {<em>id</em> &#8658; <em>12</em>}, nil, {<em>message</em> &#8658; <em>booya!</em>})</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_create_post
- assert_difference<span style="color: #990000">(</span><span style="color: #FF0000">'Post.count'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- post <span style="color: #990000">:</span>create<span style="color: #990000">,</span> <span style="color: #990000">:</span>post <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>title <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Hi'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>body <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'This is my first post.'</span><span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- assert_redirected_to post_path<span style="color: #990000">(</span>assigns<span style="color: #990000">(:</span>post<span style="color: #990000">))</span>
- assert_equal <span style="color: #FF0000">'Post was successfully created.'</span><span style="color: #990000">,</span> flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<h3 id="_testing_views">4.6. Testing Views</h3>
-<div class="para"><p>Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The <tt>assert_select</tt> assertion allows you to do this by using a simple yet powerful syntax.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">You may find references to <tt>assert_tag</tt> in other documentation, but this is now deprecated in favor of <tt>assert_select</tt>.</td>
-</tr></table>
-</div>
-<div class="para"><p>There are two forms of <tt>assert_select</tt>:</p></div>
-<div class="para"><p><tt>assert_select(selector, [equality], [message])`</tt> ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String), an expression with substitution values, or an <tt>HTML::Selector</tt> object.</p></div>
-<div class="para"><p><tt>assert_select(element, selector, [equality], [message])</tt> ensures that the equality condition is met on all the selected elements through the selector starting from the <em>element</em> (instance of <tt>HTML::Node</tt>) and its descendants.</p></div>
-<div class="para"><p>For example, you could verify the contents on the title element in your response with:</p></div>
+<div class="content">
+<pre><tt>
+NOTE: If you try running +test_should_create_post+ test from +posts_controller_test.rb+ it will fail on account of the newly added model level validation and rightly so.
+
+Let us modify +test_should_create_post+ test in +posts_controller_test.rb+ so that all our test pass:
+
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>def test_should_create_post
+ assert_difference(<em>Post.count</em>) do
+ post :create, :post &#8658; { :title &#8658; <em>Some title</em>}
+ end</p></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt> assert_redirected_to post_path(assigns(:post))
+end</tt></pre>
+</div></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_select <span style="color: #FF0000">'title'</span><span style="color: #990000">,</span> <span style="color: #FF0000">"Welcome to Rails Testing Guide"</span>
-</tt></pre></div></div>
-<div class="para"><p>You can also use nested <tt>assert_select</tt> blocks. In this case the inner <tt>assert_select</tt> will run the assertion on each element selected by the outer <tt>assert_select</tt> block:</p></div>
+<div class="content">
+<pre><tt>
+Now you can try running all the tests and they should pass.
+
+=== Available Request Types for Functional Tests ===
+
+If you're familiar with the HTTP protocol, you'll know that +get+ is a type of request. There are 5 request types supported in Rails functional tests:
+
+* +get+
+* +post+
+* +put+
+* +head+
+* +delete+
+
+All of request types are methods that you can use, however, you'll probably end up using the first two more often than the others.
+
+=== The 4 Hashes of the Apocalypse ===
+
+After a request has been made by using one of the 5 methods (+get+, +post+, etc.) and processed, you will have 4 Hash objects ready for use:
+
+* +assigns+ - Any objects that are stored as instance variables in actions for use in views.
+* +cookies+ - Any cookies that are set.
+* +flash+ - Any objects living in the flash.
+* +session+ - Any object living in session variables.
+
+As is the case with normal Hash objects, you can access the values by referencing the keys by string. You can also reference them by symbol name, except for +assigns+. For example:
+
+[source,ruby]</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt>flash["gordon"] flash[:gordon]
+session["shmession"] session[:shmession]
+cookies["are_good_for_u"] cookies[:are_good_for_u]</tt></pre>
+</div></div>
+<div class="para"><p># Because you can't use assigns[:something] for historical reasons:
+ assigns["something"] assigns(:something)</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_select <span style="color: #FF0000">'ul.navigation'</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- assert_select <span style="color: #FF0000">'li.menu_item'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>assert_select</tt> assertion is quite powerful. For more advanced usage, refer to its <a href="http://api.rubyonrails.com/classes/ActionController/Assertions/SelectorAssertions.html#M000749">documentation</a>.</p></div>
-<h4 id="_additional_view_based_assertions">4.6.1. Additional View-based Assertions</h4>
-<div class="para"><p>There are more assertions that are primarily used in testing views:</p></div>
-<div class="tableblock">
-<table rules="all"
-frame="hsides"
-cellspacing="0" cellpadding="4">
-<col width="948" />
-<col width="640" />
-<thead>
- <tr>
- <th align="left">
- Assertion
- </th>
- <th align="left">
- Purpose
- </th>
- </tr>
-</thead>
-<tbody valign="top">
- <tr>
- <td align="left">
- <tt>assert_select_email</tt>
- </td>
- <td align="left">
- Allows you to make assertions on the body of an e-mail.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_select_rjs</tt>
- </td>
- <td align="left">
- Allows you to make assertions on RJS response. <tt>assert_select_rjs</tt> has variants which allow you to narrow down on the updated element or even a particular operation on an element.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>assert_select_encoded</tt>
- </td>
- <td align="left">
- Allows you to make assertions on encoded HTML. It does this by un-encoding the contents of each element and then calling the block with all the un-encoded elements.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>css_select(selector)</tt> or <tt>css_select(element, selector)</tt>
- </td>
- <td align="left">
- Returns an array of all the elements selected by the <em>selector</em>. In the second variant it first matches the base <em>element</em> and tries to match the <em>selector</em> expression on any of its children. If there are no matches both variants return an empty array.
- </td>
- </tr>
-</tbody>
-</table>
-</div>
-<div class="para"><p>Here's an example of using <tt>assert_select_email</tt>:</p></div>
+<div class="content">
+<pre><tt>
+=== Instance Variables Available ===
+
+You also have access to three instance variables in your functional tests:
+
+* +@controller+ - The controller processing the request
+* +@request+ - The request
+* +@response+ - The response
+
+=== A Fuller Functional Test Example
+
+Here's another example that uses +flash+, +assert_redirected_to+, and +assert_difference+:
+
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>def test_should_create_post
+ assert_difference(<em>Post.count</em>) do
+ post :create, :post &#8658; { :title &#8658; <em>Hi</em>, :body &#8658; <em>This is my first post.</em>}
+ end
+ assert_redirected_to post_path(assigns(:post))
+ assert_equal <em>Post was successfully created.</em>, flash[:notice]
+end</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_select_email <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- assert_select <span style="color: #FF0000">'small'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'Please click the "Unsubscribe" link if you want to opt-out.'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-</div>
-<h2 id="_integration_testing">5. Integration Testing</h2>
-<div class="sectionbody">
-<div class="para"><p>Integration tests are used to test the interaction among any number of controllers. They are generally used to test important work flows within your application.</p></div>
-<div class="para"><p>Unlike Unit and Functional tests, integration tests have to be explicitly created under the <em>test/integration</em> folder within your application. Rails provides a generator to create an integration test skeleton for you.</p></div>
+<div class="content">
+<pre><tt>
+=== Testing Views ===
+
+Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The +assert_select+ assertion allows you to do this by using a simple yet powerful syntax.
+
+NOTE: You may find references to +assert_tag+ in other documentation, but this is now deprecated in favor of +assert_select+.
+
+There are two forms of +assert_select+:
+
++assert_select(selector, [equality], [message])`+ ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String), an expression with substitution values, or an +HTML::Selector+ object.
+
++assert_select(element, selector, [equality], [message])+ ensures that the equality condition is met on all the selected elements through the selector starting from the _element_ (instance of +HTML::Node+) and its descendants.
+
+For example, you could verify the contents on the title element in your response with:
+
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>assert_select <em>title</em>, "Welcome to Rails Testing Guide"</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/generate integration_test user_flows
- exists test/integration<span style="color: #990000">/</span>
- create test/integration/user_flows_test<span style="color: #990000">.</span>rb
-</tt></pre></div></div>
-<div class="para"><p>Here's what a freshly-generated integration test looks like:</p></div>
+<div class="content">
+<pre><tt>
+You can also use nested +assert_select+ blocks. In this case the inner +assert_select+ will run the assertion on each element selected by the outer `assert_select` block:
+
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>assert_select <em>ul.navigation</em> do
+ assert_select <em>li.menu_item</em>
+end</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
+<div class="content">
+<pre><tt>
+The +assert_select+ assertion is quite powerful. For more advanced usage, refer to its link:http://api.rubyonrails.com/classes/ActionController/Assertions/SelectorAssertions.html#M000749[documentation].
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserFlowsTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>IntegrationTest
- <span style="font-style: italic"><span style="color: #9A1900"># fixtures :your, :models</span></span>
+==== Additional View-based Assertions ====
- <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_truth
- assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Integration tests inherit from <tt>ActionController::IntegrationTest</tt>. This makes available some additional helpers to use in your integration tests. Also you need to explicitly include the fixtures to be made available to the test.</p></div>
-<h3 id="_helpers_available_for_integration_tests">5.1. Helpers Available for Integration tests</h3>
-<div class="para"><p>In addition to the standard testing helpers, there are some additional helpers available to integration tests:</p></div>
-<div class="tableblock">
-<table rules="all"
-frame="hsides"
-cellspacing="0" cellpadding="4">
-<col width="948" />
-<col width="640" />
-<thead>
- <tr>
- <th align="left">
- Helper
- </th>
- <th align="left">
- Purpose
- </th>
- </tr>
-</thead>
-<tbody valign="top">
- <tr>
- <td align="left">
- <tt>https?</tt>
- </td>
- <td align="left">
- Returns <tt>true</tt> if the session is mimicking a secure HTTPS request.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>https!</tt>
- </td>
- <td align="left">
- Allows you to mimic a secure HTTPS request.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>host!</tt>
- </td>
- <td align="left">
- Allows you to set the host name to use in the next request.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>redirect?</tt>
- </td>
- <td align="left">
- Returns <tt>true</tt> if the last request was a redirect.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>follow_redirect!</tt>
- </td>
- <td align="left">
- Follows a single redirect response.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>request_via_redirect(http_method, path, [parameters], [headers])</tt>
- </td>
- <td align="left">
- Allows you to make an HTTP request and follow any subsequent redirects.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>post_via_redirect(path, [parameters], [headers])</tt>
- </td>
- <td align="left">
- Allows you to make an HTTP POST request and follow any subsequent redirects.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>get_via_redirect(path, [parameters], [headers])</tt>
- </td>
- <td align="left">
- Allows you to make an HTTP GET request and follow any subsequent redirects.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>put_via_redirect(path, [parameters], [headers])</tt>
- </td>
- <td align="left">
- Allows you to make an HTTP PUT request and follow any subsequent redirects.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>delete_via_redirect(path, [parameters], [headers])</tt>
- </td>
- <td align="left">
- Allows you to make an HTTP DELETE request and follow any subsequent redirects.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>open_session</tt>
- </td>
- <td align="left">
- Opens a new session instance.
- </td>
- </tr>
-</tbody>
-</table>
-</div>
-<h3 id="_integration_testing_examples">5.2. Integration Testing Examples</h3>
-<div class="para"><p>A simple integration test that exercises multiple controllers:</p></div>
+There are more assertions that are primarily used in testing views:
+
+[grid="all"]
+`----------------------------------------------------------------------------------`-------------------------------------------------------
+Assertion Purpose</tt></pre>
+</div></div>
+<div class="para"><p><tt>assert_select_email</tt> Allows you to make assertions on the body of an e-mail.
+<tt>assert_select_rjs</tt> Allows you to make assertions on RJS response. <tt>assert_select_rjs</tt> has variants which allow you to narrow down on the updated element or even a particular operation on an element.
+<tt>assert_select_encoded</tt> Allows you to make assertions on encoded HTML. It does this by un-encoding the contents of each element and then calling the block with all the un-encoded elements.
+<tt>css_select(selector)</tt> or <tt>css_select(element, selector)</tt> Returns an array of all the elements selected by the <em>selector</em>. In the second variant it first matches the base <em>element</em> and tries to match the <em>selector</em> expression on any of its children. If there are no matches both variants return an empty array.</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
+<div class="content">
+<pre><tt>
+Here's an example of using +assert_select_email+:
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserFlowsTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>IntegrationTest
- fixtures <span style="color: #990000">:</span>users
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>assert_select_email do
+ assert_select <em>small</em>, <em>Please click the "Unsubscribe" link if you want to opt-out.</em>
+end</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>
+== Integration Testing ==
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_login_and_browse_site
- <span style="font-style: italic"><span style="color: #9A1900"># login via https</span></span>
- https!
- get <span style="color: #FF0000">"/login"</span>
- assert_response <span style="color: #990000">:</span>success
+Integration tests are used to test the interaction among any number of controllers. They are generally used to test important work flows within your application.
- post_via_redirect <span style="color: #FF0000">"/login"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>username <span style="color: #990000">=&gt;</span> users<span style="color: #990000">(:</span>avs<span style="color: #990000">).</span>username<span style="color: #990000">,</span> <span style="color: #990000">:</span>password <span style="color: #990000">=&gt;</span> users<span style="color: #990000">(:</span>avs<span style="color: #990000">).</span>password
- assert_equal <span style="color: #FF0000">'/welcome'</span><span style="color: #990000">,</span> path
- assert_equal <span style="color: #FF0000">'Welcome avs!'</span><span style="color: #990000">,</span> flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span>
+Unlike Unit and Functional tests, integration tests have to be explicitly created under the 'test/integration' folder within your application. Rails provides a generator to create an integration test skeleton for you.
- https!<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">)</span>
- get <span style="color: #FF0000">"/posts/all"</span>
- assert_response <span style="color: #990000">:</span>success
- assert assigns<span style="color: #990000">(:</span>products<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>As you can see the integration test involves multiple controllers and exercises the entire stack from database to dispatcher. In addition you can have multiple session instances open simultaneously in a test and extend those instances with assertion methods to create a very powerful testing DSL (domain-specific language) just for your application.</p></div>
-<div class="para"><p>Here's an example of multiple sessions and custom DSL in an integration test</p></div>
+[source, shell]</tt></pre>
+</div></div>
+<div class="para"><p>$ script/generate integration_test user_flows
+ exists test/integration/
+ create test/integration/user_flows_test.rb</p></div>
<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
+<div class="content">
+<pre><tt>
+Here's what a freshly-generated integration test looks like:
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserFlowsTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>IntegrationTest
- fixtures <span style="color: #990000">:</span>users
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>require <em>test_helper</em></p></div>
+<div class="para"><p>class UserFlowsTest &lt; ActionController::IntegrationTest
+ # fixtures :your, :models</p></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt> # Replace this with your real tests.
+ def test_truth
+ assert true
+ end
+end</tt></pre>
+</div></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>
+Integration tests inherit from +ActionController::IntegrationTest+. This makes available some additional helpers to use in your integration tests. Also you need to explicitly include the fixtures to be made available to the test.
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_login_and_browse_site
+=== Helpers Available for Integration tests ===
- <span style="font-style: italic"><span style="color: #9A1900"># User avs logs in</span></span>
- avs <span style="color: #990000">=</span> login<span style="color: #990000">(:</span>avs<span style="color: #990000">)</span>
- <span style="font-style: italic"><span style="color: #9A1900"># User guest logs in</span></span>
- guest <span style="color: #990000">=</span> login<span style="color: #990000">(:</span>guest<span style="color: #990000">)</span>
+In addition to the standard testing helpers, there are some additional helpers available to integration tests:
- <span style="font-style: italic"><span style="color: #9A1900"># Both are now available in different sessions</span></span>
- assert_equal <span style="color: #FF0000">'Welcome avs!'</span><span style="color: #990000">,</span> avs<span style="color: #990000">.</span>flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span>
- assert_equal <span style="color: #FF0000">'Welcome guest!'</span><span style="color: #990000">,</span> guest<span style="color: #990000">.</span>flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span>
+[grid="all"]
+`----------------------------------------------------------------------------------`-------------------------------------------------------
+Helper Purpose</tt></pre>
+</div></div>
+<div class="para"><p><tt>https?</tt> Returns <tt>true</tt> if the session is mimicking a secure HTTPS request.
+<tt>https!</tt> Allows you to mimic a secure HTTPS request.
+<tt>host!</tt> Allows you to set the host name to use in the next request.
+<tt>redirect?</tt> Returns <tt>true</tt> if the last request was a redirect.
+<tt>follow_redirect!</tt> Follows a single redirect response.
+<tt>request_via_redirect(http_method, path, [parameters], [headers])</tt> Allows you to make an HTTP request and follow any subsequent redirects.
+<tt>post_via_redirect(path, [parameters], [headers])</tt> Allows you to make an HTTP POST request and follow any subsequent redirects.
+<tt>get_via_redirect(path, [parameters], [headers])</tt> Allows you to make an HTTP GET request and follow any subsequent redirects.
+<tt>put_via_redirect(path, [parameters], [headers])</tt> Allows you to make an HTTP PUT request and follow any subsequent redirects.
+<tt>delete_via_redirect(path, [parameters], [headers])</tt> Allows you to make an HTTP DELETE request and follow any subsequent redirects.
+<tt>open_session</tt> Opens a new session instance.</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>
+=== Integration Testing Examples ===
- <span style="font-style: italic"><span style="color: #9A1900"># User avs can browse site</span></span>
- avs<span style="color: #990000">.</span>browses_site
- <span style="font-style: italic"><span style="color: #9A1900"># User guest can browse site aswell</span></span>
- guest<span style="color: #990000">.</span>browses_site
+A simple integration test that exercises multiple controllers:
- <span style="font-style: italic"><span style="color: #9A1900"># Continue with other assertions</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>require <em>test_helper</em></p></div>
+<div class="para"><p>class UserFlowsTest &lt; ActionController::IntegrationTest
+ fixtures :users</p></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt>def test_login_and_browse_site
+ # login via https
+ https!
+ get "/login"
+ assert_response :success</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt>post_via_redirect "/login", :username =&gt; users(:avs).username, :password =&gt; users(:avs).password
+assert_equal '/welcome', path
+assert_equal 'Welcome avs!', flash[:notice]</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt> https!(false)
+ get "/posts/all"
+ assert_response :success
+ assert assigns(:products)
+ end
+end</tt></pre>
+</div></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>
+As you can see the integration test involves multiple controllers and exercises the entire stack from database to dispatcher. In addition you can have multiple session instances open simultaneously in a test and extend those instances with assertion methods to create a very powerful testing DSL (domain-specific language) just for your application.
- private
+Here's an example of multiple sessions and custom DSL in an integration test
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> CustomDsl
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> browses_site
- get <span style="color: #FF0000">"/products/all"</span>
- assert_response <span style="color: #990000">:</span>success
- assert assigns<span style="color: #990000">(:</span>products<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+[source,ruby]</tt></pre>
+</div></div>
+<div class="para"><p>require <em>test_helper</em></p></div>
+<div class="para"><p>class UserFlowsTest &lt; ActionController::IntegrationTest
+ fixtures :users</p></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt>def test_login_and_browse_site</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt># User avs logs in
+avs = login(:avs)
+# User guest logs in
+guest = login(:guest)</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt># Both are now available in different sessions
+assert_equal 'Welcome avs!', avs.flash[:notice]
+assert_equal 'Welcome guest!', guest.flash[:notice]</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt># User avs can browse site
+avs.browses_site
+# User guest can browse site aswell
+guest.browses_site</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt> # Continue with other assertions
+end</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt>private</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt>module CustomDsl
+ def browses_site
+ get "/products/all"
+ assert_response :success
+ assert assigns(:products)
+ end
+end</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt> def login(user)
+ open_session do |sess|
+ sess.extend(CustomDsl)
+ u = users(user)
+ sess.https!
+ sess.post "/login", :username =&gt; u.username, :password =&gt; u.password
+ assert_equal '/welcome', path
+ sess.https!(false)
+ end
+ end
+end</tt></pre>
+</div></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>
+== Rake Tasks for Running your Tests ==
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> login<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
- open_session <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>sess<span style="color: #990000">|</span>
- sess<span style="color: #990000">.</span>extend<span style="color: #990000">(</span>CustomDsl<span style="color: #990000">)</span>
- u <span style="color: #990000">=</span> users<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
- sess<span style="color: #990000">.</span>https!
- sess<span style="color: #990000">.</span>post <span style="color: #FF0000">"/login"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>username <span style="color: #990000">=&gt;</span> u<span style="color: #990000">.</span>username<span style="color: #990000">,</span> <span style="color: #990000">:</span>password <span style="color: #990000">=&gt;</span> u<span style="color: #990000">.</span>password
- assert_equal <span style="color: #FF0000">'/welcome'</span><span style="color: #990000">,</span> path
- sess<span style="color: #990000">.</span>https!<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-</div>
-<h2 id="_rake_tasks_for_running_your_tests">6. Rake Tasks for Running your Tests</h2>
-<div class="sectionbody">
-<div class="para"><p>You don't need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of rake tasks to help in testing. The table below lists all rake tasks that come along in the default Rakefile when you initiate a Rail project.</p></div>
-<div class="para"><p>--------------------------------`----------------------------------------------------
-Tasks Description</p></div>
+You don't need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of rake tasks to help in testing. The table below lists all rake tasks that come along in the default Rakefile when you initiate a Rail project.
+
+[grid="all"]</tt></pre>
+</div></div>
+<div class="para"><p>Tasks Description</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>+rake test+ Runs all unit, functional and integration tests. You can also simply run +rake+ as the _test_ target is the default.
@@ -1584,7 +1146,7 @@ Tasks Description</p></div>
+rake test:plugins+ Run all the plugin tests from +vendor/plugins/*/**/test+ (or specify with +PLUGIN=_name_+)</tt></pre>
</div></div>
</div>
-<h2 id="_brief_note_about_test_unit">7. Brief Note About Test::Unit</h2>
+<h2 id="_brief_note_about_test_unit">4. Brief Note About Test::Unit</h2>
<div class="sectionbody">
<div class="para"><p>Ruby ships with a boat load of libraries. One little gem of a library is <tt>Test::Unit</tt>, a framework for unit testing in Ruby. All the basic assertions discussed above are actually defined in <tt>Test::Unit::Assertions</tt>. The class <tt>ActiveSupport::TestCase</tt> which we have been using in our unit and functional tests extends <tt>Test::Unit::TestCase</tt> that it is how we can use all the basic assertions in our tests.</p></div>
<div class="admonitionblock">
@@ -1596,7 +1158,7 @@ Tasks Description</p></div>
</tr></table>
</div>
</div>
-<h2 id="_setup_and_teardown">8. Setup and Teardown</h2>
+<h2 id="_setup_and_teardown">5. Setup and Teardown</h2>
<div class="sectionbody">
<div class="para"><p>If you would like to run a block of code before the start of each test and another block of code after the end of each test you have two special callbacks for your rescue. Let's take note of this by looking at an example for our functional test in <tt>Posts</tt> controller:</p></div>
<div class="listingblock">
@@ -1704,7 +1266,7 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
</div>
-<h2 id="_testing_routes">9. Testing Routes</h2>
+<h2 id="_testing_routes">6. Testing Routes</h2>
<div class="sectionbody">
<div class="para"><p>Like everything else in you Rails application, it's recommended to test you routes. An example test for a route in the default <tt>show</tt> action of <tt>Posts</tt> controller above should look like:</p></div>
<div class="listingblock">
@@ -1717,10 +1279,10 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
</div>
-<h2 id="_testing_your_mailers">10. Testing Your Mailers</h2>
+<h2 id="_testing_your_mailers">7. Testing Your Mailers</h2>
<div class="sectionbody">
<div class="para"><p>Testing mailer classes requires some specific tools to do a thorough job.</p></div>
-<h3 id="_keeping_the_postman_in_check">10.1. Keeping the Postman in Check</h3>
+<h3 id="_keeping_the_postman_in_check">7.1. Keeping the Postman in Check</h3>
<div class="para"><p>Your <tt>ActionMailer</tt> classes &#8212; like every other part of your Rails application &#8212; should be tested to ensure that it is working as expected.</p></div>
<div class="para"><p>The goals of testing your <tt>ActionMailer</tt> classes are to ensure that:</p></div>
<div class="ilist"><ul>
@@ -1740,14 +1302,14 @@ the right emails are being sent at the right times
</p>
</li>
</ul></div>
-<h4 id="_from_all_sides">10.1.1. From All Sides</h4>
+<h4 id="_from_all_sides">7.1.1. From All Sides</h4>
<div class="para"><p>There are two aspects of testing your mailer, the unit tests and the functional tests. In the unit tests, you run the mailer in isolation with tightly controlled inputs and compare the output to a knownvalue (a fixture &#8212; yay! more fixtures!). In the functional tests you don't so much test the minute details produced by the mailer Instead we test that our controllers and models are using the mailer in the right way. You test to prove that the right email was sent at the right time.</p></div>
-<h3 id="_unit_testing">10.2. Unit Testing</h3>
+<h3 id="_unit_testing">7.2. Unit Testing</h3>
<div class="para"><p>In order to test that your mailer is working as expected, you can use unit tests to compare the actual results of the mailer with pre-written examples of what should be produced.</p></div>
-<h4 id="_revenge_of_the_fixtures">10.2.1. Revenge of the Fixtures</h4>
+<h4 id="_revenge_of_the_fixtures">7.2.1. Revenge of the Fixtures</h4>
<div class="para"><p>For the purposes of unit testing a mailer, fixtures are used to provide an example of how the output <em>should</em> look. Because these are example emails, and not Active Record data like the other fixtures, they are kept in their own subdirectory apart from the other fixtures. The name of the directory within <tt>test/fixtures</tt> directly corresponds to the name of the mailer. So, for a mailer named <tt>UserMailer</tt>, the fixtures should reside in <tt>test/fixtures/user_mailer</tt> directory.</p></div>
<div class="para"><p>When you generated your mailer, the generator creates stub fixtures for each of the mailers actions. If you didn't use the generator you'll have to make those files yourself.</p></div>
-<h4 id="_the_basic_test_case">10.2.2. The Basic Test case</h4>
+<h4 id="_the_basic_test_case">7.2.2. The Basic Test case</h4>
<div class="para"><p>Here's a unit test to test a mailer named <tt>UserMailer</tt> whose action <tt>invite</tt> is used to send an invitation to a friend. It is an adapted version of the base test created by the generator for an <tt>invite</tt> action.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -1782,7 +1344,7 @@ Cheers!</tt></pre>
</div></div>
<div class="para"><p>This is the right time to understand a little more about writing tests for your mailers. The line <tt>ActionMailer::Base.delivery_method = :test</tt> in <tt>config/environments/test.rb</tt> sets the delivery method to test mode so that email will not actually be delivered (useful to avoid spamming your users while testing) but instead it will be appended to an array (<tt>ActionMailer::Base.deliveries</tt>).</p></div>
<div class="para"><p>However often in unit tests, mails will not actually be sent, simply constructed, as in the example above, where the precise content of the email is checked against what it should be.</p></div>
-<h3 id="_functional_testing">10.3. Functional Testing</h3>
+<h3 id="_functional_testing">7.3. Functional Testing</h3>
<div class="para"><p>Functional testing for mailers involves more than just checking that the email body, recipients and so forth are correct. In functional mail tests you call the mail deliver methods and check that the appropriate emails have been appended to the delivery list. It is fairly safe to assume that the deliver methods themselves do their job You are probably more interested in is whether your own business logic is sending emails when you expect them to got out. For example, you can check that the invite friend operation is sending an email appropriately:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -1805,7 +1367,7 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
</div>
-<h2 id="_other_testing_approaches">11. Other Testing Approaches</h2>
+<h2 id="_other_testing_approaches">8. Other Testing Approaches</h2>
<div class="sectionbody">
<div class="para"><p>The built-in <tt>test/unit</tt> based testing is not the only way to test Rails applications. Rails developers have come up with a wide variety of other approaches and aids for testing, including:</p></div>
<div class="ilist"><ul>
@@ -1831,7 +1393,7 @@ link: <a href="http://rspec.info/">RSpec</a>, a behavior-driven development fram
</li>
</ul></div>
</div>
-<h2 id="_changelog">12. Changelog</h2>
+<h2 id="_changelog">9. Changelog</h2>
<div class="sectionbody">
<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/8">Lighthouse ticket</a></p></div>
<div class="ilist"><ul>