Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / html / testing_rails_applications.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5 <title>A Guide to Testing Rails Applications</title>
6 <!--[if lt IE 8]>
7 <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
8 <![endif]-->
9 <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
10 <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
11 <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
12 <style type="text/css">
13 div#container {
14 max-width: 900px;
15 padding-bottom: 3em;
16 }
17
18 div#content {
19 margin-left: 200px;
20 }
21
22 div#container.notoc {
23 max-width: 600px;
24 }
25
26 .notoc div#content {
27 margin-left: 0;
28 }
29
30 pre {
31 line-height: 1.4em;
32 }
33
34 #content p tt {
35 background: #eeeeee;
36 border: solid 1px #cccccc;
37 padding: 3px;
38 }
39
40 dt {
41 font-weight: bold;
42 }
43
44 #content dt tt {
45 font-size: 10pt;
46 }
47
48 dd {
49 margin-left: 3em;
50 }
51
52 #content dt tt, #content pre tt {
53 background: none;
54 padding: 0;
55 border: 0;
56 }
57
58 #content .olist ol {
59 margin-left: 2em;
60 }
61
62 #header {
63 position: relative;
64 max-width: 840px;
65 margin-left: auto;
66 margin-right: auto;
67 }
68
69 #header.notoc {
70 max-width: 580px;
71 }
72
73 #logo {
74 position: absolute;
75 left: 10px;
76 top: 10px;
77 width: 110px;
78 height: 140px;
79 }
80
81 div#header h1#site_title {
82 background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
83 position: absolute;
84 width: 392px;
85 height: 55px;
86 left: 145px;
87 top: 20px;
88 margin: 0;
89 padding: 0;
90 }
91
92 #site_title span {
93 display: none;
94 }
95
96 #site_title_tagline {
97 display: none;
98 }
99
100 ul#navMain {
101 position: absolute;
102 margin: 0;
103 padding: 0;
104 top: 97px;
105 left: 145px;
106 }
107
108 .left-floaty, .right-floaty {
109 padding: 15px;
110 }
111
112 .admonitionblock,
113 .tableblock {
114 margin-left: 1em;
115 margin-right: 1em;
116 margin-top: 0.25em;
117 margin-bottom: 1em;
118 }
119
120 .admonitionblock .icon {
121 padding-right: 8px;
122 }
123
124 .admonitionblock .content {
125 border: solid 1px #ffda78;
126 background: #fffebd;
127 padding: 10px;
128 padding-top: 8px;
129 padding-bottom: 8px;
130 }
131
132 .admonitionblock .title {
133 font-size: 140%;
134 margin-bottom: 0.5em;
135 }
136
137 .tableblock table {
138 border: solid 1px #aaaaff;
139 background: #f0f0ff;
140 }
141
142 .tableblock th {
143 background: #e0e0e0;
144 }
145
146 .tableblock th,
147 .tableblock td {
148 padding: 3px;
149 padding-left: 5px;
150 padding-right: 5px;
151 }
152
153 .sidebarblock {
154 margin-top: 0.25em;
155 margin: 1em;
156 border: solid 1px #ccccbb;
157 padding: 8px;
158 background: #ffffe0;
159 }
160
161 .sidebarblock .sidebar-title {
162 font-size: 140%;
163 font-weight: 600;
164 margin-bottom: 0.3em;
165 }
166
167 .sidebarblock .sidebar-content > .para:last-child > p {
168 margin-bottom: 0;
169 }
170
171 .sidebarblock .sidebar-title a {
172 text-decoration: none;
173 }
174
175 .sidebarblock .sidebar-title a:hover {
176 text-decoration: underline;
177 }
178
179 </style>
180 </head>
181 <body>
182 <div id="header" >
183 <div id="logo">
184 <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
185 </div>
186
187 <h1 id="site_title"><span>Ruby on Rails</span></h1>
188 <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
189
190 <ul id="navMain">
191 <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
192 <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
193 </ul>
194 </div>
195
196 <div id="container">
197
198 <div id="sidebar">
199 <h2>Chapters</h2>
200 <ol>
201 <li>
202 <a href="#_why_write_tests_for_your_rails_applications">Why Write Tests for your Rails Applications?</a>
203 </li>
204 <li>
205 <a href="#_introduction_to_testing">Introduction to Testing</a>
206 <ul>
207
208 <li><a href="#_the_3_environments">The 3 Environments</a></li>
209
210 <li><a href="#_rails_sets_up_for_testing_from_the_word_go">Rails Sets up for Testing from the Word Go</a></li>
211
212 <li><a href="#_the_low_down_on_fixtures">The Low-Down on Fixtures</a></li>
213
214 </ul>
215 </li>
216 <li>
217 <a href="#_unit_testing_your_models">Unit Testing your Models</a>
218 <ul>
219
220 <li><a href="#_preparing_you_application_for_testing">Preparing you Application for Testing</a></li>
221
222 <li><a href="#_running_tests">Running Tests</a></li>
223
224 <li><a href="#_what_to_include_in_your_unit_tests">What to Include in Your Unit Tests</a></li>
225
226 <li><a href="#_assertions_available">Assertions Available</a></li>
227
228 <li><a href="#_rails_specific_assertions">Rails Specific Assertions</a></li>
229
230 </ul>
231 </li>
232 <li>
233 <a href="#_functional_tests_for_your_controllers">Functional Tests for Your Controllers</a>
234 <ul>
235
236 <li><a href="#_what_to_include_in_your_functional_tests">What to include in your Functional Tests</a></li>
237
238 <li><a href="#_available_request_types_for_functional_tests">Available Request Types for Functional Tests</a></li>
239
240 <li><a href="#_the_4_hashes_of_the_apocalypse">The 4 Hashes of the Apocalypse</a></li>
241
242 <li><a href="#_instance_variables_available">Instance Variables Available</a></li>
243
244 <li><a href="#_a_fuller_functional_test_example">A Fuller Functional Test Example</a></li>
245
246 <li><a href="#_testing_views">Testing Views</a></li>
247
248 </ul>
249 </li>
250 <li>
251 <a href="#_integration_testing">Integration Testing</a>
252 <ul>
253
254 <li><a href="#_helpers_available_for_integration_tests">Helpers Available for Integration tests</a></li>
255
256 <li><a href="#_integration_testing_examples">Integration Testing Examples</a></li>
257
258 </ul>
259 </li>
260 <li>
261 <a href="#_rake_tasks_for_running_your_tests">Rake Tasks for Running your Tests</a>
262 </li>
263 <li>
264 <a href="#_brief_note_about_test_unit">Brief Note About Test::Unit</a>
265 </li>
266 <li>
267 <a href="#_setup_and_teardown">Setup and Teardown</a>
268 </li>
269 <li>
270 <a href="#_testing_routes">Testing Routes</a>
271 </li>
272 <li>
273 <a href="#_testing_your_mailers">Testing Your Mailers</a>
274 <ul>
275
276 <li><a href="#_keeping_the_postman_in_check">Keeping the Postman in Check</a></li>
277
278 <li><a href="#_unit_testing">Unit Testing</a></li>
279
280 <li><a href="#_functional_testing">Functional Testing</a></li>
281
282 </ul>
283 </li>
284 <li>
285 <a href="#_other_testing_approaches">Other Testing Approaches</a>
286 </li>
287 <li>
288 <a href="#_changelog">Changelog</a>
289 </li>
290 </ol>
291 </div>
292
293 <div id="content">
294 <h1>A Guide to Testing Rails Applications</h1>
295 <div id="preamble">
296 <div class="sectionbody">
297 <div class="para"><p>This guide covers built-in mechanisms offered by Rails to test your application. By referring to this guide, you will be able to:</p></div>
298 <div class="ilist"><ul>
299 <li>
300 <p>
301 Understand Rails testing terminology
302 </p>
303 </li>
304 <li>
305 <p>
306 Write unit, functional and integration tests for your application
307 </p>
308 </li>
309 <li>
310 <p>
311 Identify other popular testing approaches and plugins
312 </p>
313 </li>
314 </ul></div>
315 <div class="para"><p>This guide won't teach you to write a Rails application; it assumes basic familiarity with the Rails way of doing things.</p></div>
316 </div>
317 </div>
318 <h2 id="_why_write_tests_for_your_rails_applications">1. Why Write Tests for your Rails Applications?</h2>
319 <div class="sectionbody">
320 <div class="ilist"><ul>
321 <li>
322 <p>
323 Rails makes it super easy to write your tests. It starts by producing skeleton test code in background while you are creating your models and controllers.
324 </p>
325 </li>
326 <li>
327 <p>
328 By simply running your Rails tests you can ensure your code adheres to the desired functionality even after some major code refactoring.
329 </p>
330 </li>
331 <li>
332 <p>
333 Rails tests can also simulate browser requests and thus you can test your application's response without having to test it through your browser.
334 </p>
335 </li>
336 </ul></div>
337 </div>
338 <h2 id="_introduction_to_testing">2. Introduction to Testing</h2>
339 <div class="sectionbody">
340 <div class="para"><p>Testing support was woven into the Rails fabric from the beginning. It wasn't an "oh! let's bolt on support for running tests because they're new and cool" epiphany. Just about every Rails application interacts heavily with a database - and, as a result, your tests will need a database to interact with as well. To write efficient tests, you'll need to understand how to set up this database and populate it with sample data.</p></div>
341 <h3 id="_the_3_environments">2.1. The 3 Environments</h3>
342 <div class="para"><p>Every Rails application you build has 3 sides: a side for production, a side for development, and a side for testing.</p></div>
343 <div class="para"><p>One place you'll find this distinction is in the <tt>config/database.yml</tt> file. This YAML configuration file has 3 different sections defining 3 unique database setups:</p></div>
344 <div class="ilist"><ul>
345 <li>
346 <p>
347 production
348 </p>
349 </li>
350 <li>
351 <p>
352 development
353 </p>
354 </li>
355 <li>
356 <p>
357 test
358 </p>
359 </li>
360 </ul></div>
361 <div class="para"><p>This allows you to set up and interact with test data without any danger of your tests altering data from your production environment.</p></div>
362 <div class="para"><p>For example, suppose you need to test your new <tt>delete_this_user_and_every_everything_associated_with_it</tt> function. Wouldn't you want to run this in an environment where it makes no difference if you destroy data or not?</p></div>
363 <div class="para"><p>When you do end up destroying your testing database (and it will happen, trust me), you can rebuild it from scratch according to the specs defined in the development database. You can do this by running <tt>rake db:test:prepare</tt>.</p></div>
364 <h3 id="_rails_sets_up_for_testing_from_the_word_go">2.2. Rails Sets up for Testing from the Word Go</h3>
365 <div class="para"><p>Rails creates a <tt>test</tt> folder for you as soon as you create a Rails project using <tt>rails <em>application_name</em></tt>. If you list the contents of this folder then you shall see:</p></div>
366 <div class="listingblock">
367 <div class="content"><!-- Generator: GNU source-highlight 2.9
368 by Lorenzo Bettini
369 http://www.lorenzobettini.it
370 http://www.gnu.org/software/src-highlite -->
371 <pre><tt>$ ls -F <span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">/</span>
372
373 fixtures<span style="color: #990000">/</span> functional<span style="color: #990000">/</span> integration<span style="color: #990000">/</span> test_helper<span style="color: #990000">.</span>rb unit<span style="color: #990000">/</span>
374 </tt></pre></div></div>
375 <div class="para"><p>The <tt>unit</tt> folder is meant to hold tests for your models, the <tt>functional</tt> folder is meant to hold tests for your controllers, and the <tt>integration</tt> folder is meant to hold tests that involve any number of controllers interacting. Fixtures are a way of organizing test data; they reside in the <tt>fixtures</tt> folder. The <tt>test_helper.rb</tt> file holds the default configuration for your tests.</p></div>
376 <h3 id="_the_low_down_on_fixtures">2.3. The Low-Down on Fixtures</h3>
377 <div class="para"><p>For good tests, you'll need to give some thought to setting up test data. In Rails, you can handle this by defining and customizing fixtures.</p></div>
378 <h4 id="_what_are_fixtures">2.3.1. What Are Fixtures?</h4>
379 <div class="para"><p><em>Fixtures</em> is a fancy word for sample data. Fixtures allow you to populate your testing database with predefined data before your tests run. Fixtures are database independent and assume one of two formats: <strong>YAML</strong> or <strong>CSV</strong>. In this guide we will use <strong>YAML</strong> which is the preferred format.</p></div>
380 <div class="para"><p>You'll find fixtures under your <tt>test/fixtures</tt> directory. When you run <tt>script/generate model</tt> to create a new model, fixture stubs will be automatically created and placed in this directory.</p></div>
381 <h4 id="_yaml">2.3.2. YAML</h4>
382 <div class="para"><p>YAML-formatted fixtures are a very human-friendly way to describe your sample data. These types of fixtures have the <strong>.yml</strong> file extension (as in <tt>users.yml</tt>).</p></div>
383 <div class="para"><p>Here's a sample YAML fixture file:</p></div>
384 <div class="listingblock">
385 <div class="content"><!-- Generator: GNU source-highlight 2.9
386 by Lorenzo Bettini
387 http://www.lorenzobettini.it
388 http://www.gnu.org/software/src-highlite -->
389 <pre><tt><span style="font-style: italic"><span style="color: #9A1900"># low &amp; behold! I am a YAML comment!</span></span>
390 david<span style="color: #990000">:</span>
391 name<span style="color: #990000">:</span> David Heinemeier Hansson
392 birthday<span style="color: #990000">:</span> <span style="color: #993399">1979</span><span style="color: #990000">-</span><span style="color: #993399">10</span><span style="color: #990000">-</span><span style="color: #993399">15</span>
393 profession<span style="color: #990000">:</span> Systems development
394
395 steve<span style="color: #990000">:</span>
396 name<span style="color: #990000">:</span> Steve Ross Kellock
397 birthday<span style="color: #990000">:</span> <span style="color: #993399">1974</span><span style="color: #990000">-</span><span style="color: #993399">09</span><span style="color: #990000">-</span><span style="color: #993399">27</span>
398 profession<span style="color: #990000">:</span> guy with keyboard
399 </tt></pre></div></div>
400 <div class="para"><p>Each fixture is given a name followed by an indented list of colon-separated key/value pairs. Records are separated by a blank space. You can place comments in a fixture file by using the # character in the first column.</p></div>
401 <h4 id="_erb_in_it_up">2.3.3. ERb'in It Up</h4>
402 <div class="para"><p>ERb allows you embed ruby code within templates. Both the YAML and CSV fixture formats are pre-processed with ERb when you load fixtures. This allows you to use Ruby to help you generate some sample data.</p></div>
403 <div class="listingblock">
404 <div class="content"><!-- Generator: GNU source-highlight 2.9
405 by Lorenzo Bettini
406 http://www.lorenzobettini.it
407 http://www.gnu.org/software/src-highlite -->
408 <pre><tt><span style="color: #FF0000">&lt;% earth_size = 20 -%&gt;</span>
409 mercury<span style="color: #990000">:</span>
410 size<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= earth_size / 50 %&gt;</span>
411 brightest_on<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= 113.days.ago.to_s(:db) %&gt;</span>
412
413 venus<span style="color: #990000">:</span>
414 size<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= earth_size / 2 %&gt;</span>
415 brightest_on<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= 67.days.ago.to_s(:db) %&gt;</span>
416
417 mars<span style="color: #990000">:</span>
418 size<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= earth_size - 69 %&gt;</span>
419 brightest_on<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= 13.days.from_now.to_s(:db) %&gt;</span>
420 </tt></pre></div></div>
421 <div class="para"><p>Anything encased within the</p></div>
422 <div class="listingblock">
423 <div class="content"><!-- Generator: GNU source-highlight 2.9
424 by Lorenzo Bettini
425 http://www.lorenzobettini.it
426 http://www.gnu.org/software/src-highlite -->
427 <pre><tt><span style="color: #FF0000">&lt;% %&gt;</span>
428 </tt></pre></div></div>
429 <div class="para"><p>tag is considered Ruby code. When this fixture is loaded, the <tt>size</tt> attribute of the three records will be set to 20/50, 20/2, and 20-69 respectively. The <tt>brightest_on</tt> attribute will also be evaluated and formatted by Rails to be compatible with the database.</p></div>
430 <h4 id="_fixtures_in_action">2.3.4. Fixtures in Action</h4>
431 <div class="para"><p>Rails by default automatically loads all fixtures from the <em>test/fixtures</em> folder for your unit and functional test. Loading involves three steps:</p></div>
432 <div class="ilist"><ul>
433 <li>
434 <p>
435 Remove any existing data from the table corresponding to the fixture
436 </p>
437 </li>
438 <li>
439 <p>
440 Load the fixture data into the table
441 </p>
442 </li>
443 <li>
444 <p>
445 Dump the fixture data into a variable in case you want to access it directly
446 </p>
447 </li>
448 </ul></div>
449 <h4 id="_hashes_with_special_powers">2.3.5. Hashes with Special Powers</h4>
450 <div class="para"><p>Fixtures are basically Hash objects. As mentioned in point #3 above, you can access the hash object directly because it is automatically setup as a local variable of the test case. For example:</p></div>
451 <div class="listingblock">
452 <div class="content"><!-- Generator: GNU source-highlight 2.9
453 by Lorenzo Bettini
454 http://www.lorenzobettini.it
455 http://www.gnu.org/software/src-highlite -->
456 <pre><tt><span style="font-style: italic"><span style="color: #9A1900"># this will return the Hash for the fixture named david</span></span>
457 users<span style="color: #990000">(:</span>david<span style="color: #990000">)</span>
458
459 <span style="font-style: italic"><span style="color: #9A1900"># this will return the property for david called id</span></span>
460 users<span style="color: #990000">(:</span>david<span style="color: #990000">).</span>id
461 </tt></pre></div></div>
462 <div class="para"><p>Fixtures can also transform themselves into the form of the original class. Thus, you can get at the methods only available to that class.</p></div>
463 <div class="listingblock">
464 <div class="content"><!-- Generator: GNU source-highlight 2.9
465 by Lorenzo Bettini
466 http://www.lorenzobettini.it
467 http://www.gnu.org/software/src-highlite -->
468 <pre><tt><span style="font-style: italic"><span style="color: #9A1900"># using the find method, we grab the "real" david as a User</span></span>
469 david <span style="color: #990000">=</span> users<span style="color: #990000">(:</span>david<span style="color: #990000">).</span>find
470
471 <span style="font-style: italic"><span style="color: #9A1900"># and now we have access to methods only available to a User class</span></span>
472 email<span style="color: #990000">(</span>david<span style="color: #990000">.</span>girlfriend<span style="color: #990000">.</span>email<span style="color: #990000">,</span> david<span style="color: #990000">.</span>location_tonight<span style="color: #990000">)</span>
473 </tt></pre></div></div>
474 </div>
475 <h2 id="_unit_testing_your_models">3. Unit Testing your Models</h2>
476 <div class="sectionbody">
477 <div class="para"><p>In Rails, unit tests are what you write to test your models.</p></div>
478 <div class="para"><p>For this guide we will be using Rails <em>scaffolding</em>. It will create the model, a migration, controller and views for the new resource in a single operation. It will also create a full test suite following Rails best practises. I will be using examples from this generated code and would be supplementing it with additional examples where necessary.</p></div>
479 <div class="admonitionblock">
480 <table><tr>
481 <td class="icon">
482 <img src="./images/icons/note.png" alt="Note" />
483 </td>
484 <td class="content">For more information on Rails <em>scaffolding</em>, refer to <a href="../getting_started_with_rails.html">Getting Started with Rails</a></td>
485 </tr></table>
486 </div>
487 <div class="para"><p>When you use <tt>script/generate scaffold</tt>, for a resource among other things it creates a test stub in the <tt>test/unit</tt> folder:</p></div>
488 <div class="listingblock">
489 <div class="content">
490 <pre><tt>$ script/generate scaffold post title:string body:text
491 ...
492 create app/models/post.rb
493 create test/unit/post_test.rb
494 create test/fixtures/posts.yml
495 ...</tt></pre>
496 </div></div>
497 <div class="para"><p>The default test stub in <tt>test/unit/post_test.rb</tt> looks like this:</p></div>
498 <div class="listingblock">
499 <div class="content"><!-- Generator: GNU source-highlight 2.9
500 by Lorenzo Bettini
501 http://www.lorenzobettini.it
502 http://www.gnu.org/software/src-highlite -->
503 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
504
505 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostTest <span style="color: #990000">&lt;</span> ActiveSupport<span style="color: #990000">::</span>TestCase
506 <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
507 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_truth
508 assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
509 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
510 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
511 </tt></pre></div></div>
512 <div class="para"><p>A line by line examination of this file will help get you oriented to Rails testing code and terminology.</p></div>
513 <div class="listingblock">
514 <div class="content"><!-- Generator: GNU source-highlight 2.9
515 by Lorenzo Bettini
516 http://www.lorenzobettini.it
517 http://www.gnu.org/software/src-highlite -->
518 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
519 </tt></pre></div></div>
520 <div class="para"><p>As you know by now that <tt>test_helper.rb</tt> specifies the default configuration to run our tests. This is included with all the tests, so any methods added to this file are available to all your tests.</p></div>
521 <div class="listingblock">
522 <div class="content"><!-- Generator: GNU source-highlight 2.9
523 by Lorenzo Bettini
524 http://www.lorenzobettini.it
525 http://www.gnu.org/software/src-highlite -->
526 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostTest <span style="color: #990000">&lt;</span> ActiveSupport<span style="color: #990000">::</span>TestCase
527 </tt></pre></div></div>
528 <div class="para"><p>The <tt>PostTest</tt> class defines a <em>test case</em> because it inherits from <tt>ActiveSupport::TestCase</tt>. <tt>PostTest</tt> thus has all the methods available from <tt>ActiveSupport::TestCase</tt>. You'll see those methods a little later in this guide.</p></div>
529 <div class="listingblock">
530 <div class="content"><!-- Generator: GNU source-highlight 2.9
531 by Lorenzo Bettini
532 http://www.lorenzobettini.it
533 http://www.gnu.org/software/src-highlite -->
534 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_truth
535 </tt></pre></div></div>
536 <div class="para"><p>Any method defined within a test case that begins with <tt>test</tt> (case sensitive) is simply called a test. So, <tt>test_password</tt>, <tt>test_valid_password</tt> and <tt>testValidPassword</tt> all are legal test names and are run automatically when the test case is run.</p></div>
537 <div class="listingblock">
538 <div class="content"><!-- Generator: GNU source-highlight 2.9
539 by Lorenzo Bettini
540 http://www.lorenzobettini.it
541 http://www.gnu.org/software/src-highlite -->
542 <pre><tt>assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
543 </tt></pre></div></div>
544 <div class="para"><p>This line of code is called an <em>assertion</em>. An assertion is a line of code that evaluates an object (or expression) for expected results. For example, an assertion can check:</p></div>
545 <div class="ilist"><ul>
546 <li>
547 <p>
548 is this value = that value?
549 </p>
550 </li>
551 <li>
552 <p>
553 is this object nil?
554 </p>
555 </li>
556 <li>
557 <p>
558 does this line of code throw an exception?
559 </p>
560 </li>
561 <li>
562 <p>
563 is the user's password greater than 5 characters?
564 </p>
565 </li>
566 </ul></div>
567 <div class="para"><p>Every test contains one or more assertions. Only when all the assertions are successful the test passes.</p></div>
568 <h3 id="_preparing_you_application_for_testing">3.1. Preparing you Application for Testing</h3>
569 <div class="para"><p>Before you can run your tests you need to ensure that the test database structure is current. For this you can use the following rake commands:</p></div>
570 <div class="listingblock">
571 <div class="content"><!-- Generator: GNU source-highlight 2.9
572 by Lorenzo Bettini
573 http://www.lorenzobettini.it
574 http://www.gnu.org/software/src-highlite -->
575 <pre><tt>$ rake db<span style="color: #990000">:</span>migrate
576 <span style="color: #990000">...</span>
577 $ rake db<span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">:</span>load
578 </tt></pre></div></div>
579 <div class="para"><p>Above <tt>rake db:migrate</tt> runs any pending migrations on the <em>developemnt</em> environment and updates <tt>db/schema.rb</tt>. <tt>rake db:test:load</tt> recreates the test database from the current db/schema.rb. On subsequent attempts it is a good to first run <tt>db:test:prepare</tt> as it first checks for pending migrations and warns you appropriately.</p></div>
580 <div class="admonitionblock">
581 <table><tr>
582 <td class="icon">
583 <img src="./images/icons/note.png" alt="Note" />
584 </td>
585 <td class="content"><tt>db:test:prepare</tt> will fail with an error if db/schema.rb doesn't exists.</td>
586 </tr></table>
587 </div>
588 <h4 id="_rake_tasks_for_preparing_you_application_for_testing">3.1.1. Rake Tasks for Preparing you Application for Testing ==</h4>
589 <div class="para"><p>--------------------------------`----------------------------------------------------
590 Tasks Description</p></div>
591 <div class="listingblock">
592 <div class="content">
593 <pre><tt>+rake db:test:clone+ Recreate the test database from the current environment's database schema
594 +rake db:test:clone_structure+ Recreate the test databases from the development structure
595 +rake db:test:load+ Recreate the test database from the current +schema.rb+
596 +rake db:test:prepare+ Check for pending migrations and load the test schema
597 +rake db:test:purge+ Empty the test database.</tt></pre>
598 </div></div>
599 <div class="admonitionblock">
600 <table><tr>
601 <td class="icon">
602 <img src="./images/icons/tip.png" alt="Tip" />
603 </td>
604 <td class="content">You can see all these rake tasks and their descriptions by running <tt>rake &#8212;tasks &#8212;describe</tt></td>
605 </tr></table>
606 </div>
607 <h3 id="_running_tests">3.2. Running Tests</h3>
608 <div class="para"><p>Running a test is as simple as invoking the file containing the test cases through Ruby:</p></div>
609 <div class="listingblock">
610 <div class="content"><!-- Generator: GNU source-highlight 2.9
611 by Lorenzo Bettini
612 http://www.lorenzobettini.it
613 http://www.gnu.org/software/src-highlite -->
614 <pre><tt>$ cd <span style="font-weight: bold"><span style="color: #0000FF">test</span></span>
615 $ ruby unit/post_test<span style="color: #990000">.</span>rb
616
617 Loaded suite unit/post_test
618 Started
619 <span style="color: #990000">.</span>
620 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>
621
622 <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
623 </tt></pre></div></div>
624 <div class="para"><p>This will run all the test methods from the test case.</p></div>
625 <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>
626 <div class="listingblock">
627 <div class="content">
628 <pre><tt>$ ruby unit/post_test.rb -n test_truth
629
630 Loaded suite unit/post_test
631 Started
632 .
633 Finished in 0.023513 seconds.
634
635 1 tests, 1 assertions, 0 failures, 0 errors</tt></pre>
636 </div></div>
637 <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>
638 <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>
639 <div class="listingblock">
640 <div class="content"><!-- Generator: GNU source-highlight 2.9
641 by Lorenzo Bettini
642 http://www.lorenzobettini.it
643 http://www.gnu.org/software/src-highlite -->
644 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_not_save_post_without_title
645 post <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new
646 assert <span style="color: #990000">!</span>post<span style="color: #990000">.</span>save
647 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
648 </tt></pre></div></div>
649 <div class="para"><p>Let us run this newly added test.</p></div>
650 <div class="listingblock">
651 <div class="content">
652 <pre><tt>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
653 Loaded suite unit/post_test
654 Started
655 F
656 Finished in 0.197094 seconds.
657
658 1) Failure:
659 test_should_not_save_post_without_title(PostTest)
660 [unit/post_test.rb:11:in `test_should_not_save_post_without_title'
661 /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
662 /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
663 &lt;false&gt; is not true.
664
665 1 tests, 1 assertions, 1 failures, 0 errors</tt></pre>
666 </div></div>
667 <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>
668 <div class="listingblock">
669 <div class="content"><!-- Generator: GNU source-highlight 2.9
670 by Lorenzo Bettini
671 http://www.lorenzobettini.it
672 http://www.gnu.org/software/src-highlite -->
673 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_not_save_post_without_title
674 post <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new
675 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>
676 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
677 </tt></pre></div></div>
678 <div class="para"><p>Running this test shows the friendlier assertion message:</p></div>
679 <div class="listingblock">
680 <div class="content">
681 <pre><tt>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
682 Loaded suite unit/post_test
683 Started
684 F
685 Finished in 0.198093 seconds.
686
687 1) Failure:
688 test_should_not_save_post_without_title(PostTest)
689 [unit/post_test.rb:11:in `test_should_not_save_post_without_title'
690 /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
691 /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
692 Saved the post without a title.
693 &lt;false&gt; is not true.
694
695 1 tests, 1 assertions, 1 failures, 0 errors</tt></pre>
696 </div></div>
697 <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>
698 <div class="listingblock">
699 <div class="content"><!-- Generator: GNU source-highlight 2.9
700 by Lorenzo Bettini
701 http://www.lorenzobettini.it
702 http://www.gnu.org/software/src-highlite -->
703 <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
704 validates_presence_of <span style="color: #990000">:</span>title
705 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
706 </tt></pre></div></div>
707 <div class="para"><p>Now the test should pass. Let us verify by running the test again:</p></div>
708 <div class="listingblock">
709 <div class="content">
710 <pre><tt>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
711 Loaded suite unit/post_test
712 Started
713 .
714 Finished in 0.193608 seconds.
715
716 1 tests, 1 assertions, 0 failures, 0 errors</tt></pre>
717 </div></div>
718 <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>
719 <div class="admonitionblock">
720 <table><tr>
721 <td class="icon">
722 <img src="./images/icons/tip.png" alt="Tip" />
723 </td>
724 <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>
725 </tr></table>
726 </div>
727 <div class="para"><p>To see how an error gets reported, here's a test containing an error:</p></div>
728 <div class="listingblock">
729 <div class="content"><!-- Generator: GNU source-highlight 2.9
730 by Lorenzo Bettini
731 http://www.lorenzobettini.it
732 http://www.gnu.org/software/src-highlite -->
733 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_report_error
734 <span style="font-style: italic"><span style="color: #9A1900"># some_undefined_variable is not defined elsewhere in the test case</span></span>
735 some_undefined_variable
736 assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
737 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
738 </tt></pre></div></div>
739 <div class="para"><p>Now you can see even more output in the console from running the tests:</p></div>
740 <div class="listingblock">
741 <div class="content">
742 <pre><tt>$ ruby unit/post_test.rb -n test_should_report_error
743 Loaded suite unit/post_test
744 Started
745 E
746 Finished in 0.195757 seconds.
747
748 1) Error:
749 test_should_report_error(PostTest):
750 NameError: undefined local variable or method `some_undefined_variable' for #&lt;PostTest:0x2cc9de8&gt;
751 /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/test_process.rb:467:in `method_missing'
752 unit/post_test.rb:16:in `test_should_report_error'
753 /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
754 /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run'
755
756 1 tests, 0 assertions, 0 failures, 1 errors</tt></pre>
757 </div></div>
758 <div class="para"><p>Notice the <em>E</em> in the output. It denotes a test with error.</p></div>
759 <div class="admonitionblock">
760 <table><tr>
761 <td class="icon">
762 <img src="./images/icons/note.png" alt="Note" />
763 </td>
764 <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>
765 </tr></table>
766 </div>
767 <h3 id="_what_to_include_in_your_unit_tests">3.3. What to Include in Your Unit Tests</h3>
768 <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>
769 <h3 id="_assertions_available">3.4. Assertions Available</h3>
770 <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>
771 <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>
772 <div class="tableblock">
773 <table rules="all"
774 frame="hsides"
775 cellspacing="0" cellpadding="4">
776 <col width="754" />
777 <col width="834" />
778 <thead>
779 <tr>
780 <th align="left">
781 Assertion
782 </th>
783 <th align="left">
784 Purpose
785 </th>
786 </tr>
787 </thead>
788 <tbody valign="top">
789 <tr>
790 <td align="left">
791 <tt>assert( boolean, [msg] )</tt>
792 </td>
793 <td align="left">
794 Ensures that the object/expression is true.
795 </td>
796 </tr>
797 <tr>
798 <td align="left">
799 <tt>assert_equal( obj1, obj2, [msg] )</tt>
800 </td>
801 <td align="left">
802 Ensures that <tt>obj1 == obj2</tt> is true.
803 </td>
804 </tr>
805 <tr>
806 <td align="left">
807 <tt>assert_not_equal( obj1, obj2, [msg] )</tt>
808 </td>
809 <td align="left">
810 Ensures that <tt>obj1 == obj2</tt> is false.
811 </td>
812 </tr>
813 <tr>
814 <td align="left">
815 <tt>assert_same( obj1, obj2, [msg] )</tt>
816 </td>
817 <td align="left">
818 Ensures that <tt>obj1.equal?(obj2)</tt> is true.
819 </td>
820 </tr>
821 <tr>
822 <td align="left">
823 <tt>assert_not_same( obj1, obj2, [msg] )</tt>
824 </td>
825 <td align="left">
826 Ensures that <tt>obj1.equal?(obj2)</tt> is false.
827 </td>
828 </tr>
829 <tr>
830 <td align="left">
831 <tt>assert_nil( obj, [msg] )</tt>
832 </td>
833 <td align="left">
834 Ensures that <tt>obj.nil?</tt> is true.
835 </td>
836 </tr>
837 <tr>
838 <td align="left">
839 <tt>assert_not_nil( obj, [msg] )</tt>
840 </td>
841 <td align="left">
842 Ensures that <tt>obj.nil?</tt> is false.
843 </td>
844 </tr>
845 <tr>
846 <td align="left">
847 <tt>assert_match( regexp, string, [msg] )</tt>
848 </td>
849 <td align="left">
850 Ensures that a string matches the regular expression.
851 </td>
852 </tr>
853 <tr>
854 <td align="left">
855 <tt>assert_no_match( regexp, string, [msg] )</tt>
856 </td>
857 <td align="left">
858 Ensures that a string doesn't matches the regular expression.
859 </td>
860 </tr>
861 <tr>
862 <td align="left">
863 <tt>assert_in_delta( expecting, actual, delta, [msg] )</tt>
864 </td>
865 <td align="left">
866 Ensures that the numbers <tt>expecting</tt> and <tt>actual</tt> are within <tt>delta</tt> of each other.
867 </td>
868 </tr>
869 <tr>
870 <td align="left">
871 <tt>assert_throws( symbol, [msg] ) { block }</tt>
872 </td>
873 <td align="left">
874 Ensures that the given block throws the symbol.
875 </td>
876 </tr>
877 <tr>
878 <td align="left">
879 <tt>assert_raises( exception1, exception2, &#8230; ) { block }</tt>
880 </td>
881 <td align="left">
882 Ensures that the given block raises one of the given exceptions.
883 </td>
884 </tr>
885 <tr>
886 <td align="left">
887 <tt>assert_nothing_raised( exception1, exception2, &#8230; ) { block }</tt>
888 </td>
889 <td align="left">
890 Ensures that the given block doesn't raise one of the given exceptions.
891 </td>
892 </tr>
893 <tr>
894 <td align="left">
895 <tt>assert_instance_of( class, obj, [msg] )</tt>
896 </td>
897 <td align="left">
898 Ensures that <tt>obj</tt> is of the <tt>class</tt> type.
899 </td>
900 </tr>
901 <tr>
902 <td align="left">
903 <tt>assert_kind_of( class, obj, [msg] )</tt>
904 </td>
905 <td align="left">
906 Ensures that <tt>obj</tt> is or descends from <tt>class</tt>.
907 </td>
908 </tr>
909 <tr>
910 <td align="left">
911 <tt>assert_respond_to( obj, symbol, [msg] )</tt>
912 </td>
913 <td align="left">
914 Ensures that <tt>obj</tt> has a method called <tt>symbol</tt>.
915 </td>
916 </tr>
917 <tr>
918 <td align="left">
919 <tt>assert_operator( obj1, operator, obj2, [msg] )</tt>
920 </td>
921 <td align="left">
922 Ensures that <tt>obj1.operator(obj2)</tt> is true.
923 </td>
924 </tr>
925 <tr>
926 <td align="left">
927 <tt>assert_send( array, [msg] )</tt>
928 </td>
929 <td align="left">
930 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?
931 </td>
932 </tr>
933 <tr>
934 <td align="left">
935 <tt>flunk( [msg] )</tt>
936 </td>
937 <td align="left">
938 Ensures failure. This is useful to explicitly mark a test that isn't finished yet.
939 </td>
940 </tr>
941 </tbody>
942 </table>
943 </div>
944 <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>
945 <div class="admonitionblock">
946 <table><tr>
947 <td class="icon">
948 <img src="./images/icons/note.png" alt="Note" />
949 </td>
950 <td class="content">Creating your own assertions is an advanced topic that we won't cover in this tutorial.</td>
951 </tr></table>
952 </div>
953 <h3 id="_rails_specific_assertions">3.5. Rails Specific Assertions</h3>
954 <div class="para"><p>Rails adds some custom assertions of its own to the <tt>test/unit</tt> framework:</p></div>
955 <div class="tableblock">
956 <table rules="all"
957 frame="hsides"
958 cellspacing="0" cellpadding="4">
959 <col width="948" />
960 <col width="640" />
961 <thead>
962 <tr>
963 <th align="left">
964 Assertion
965 </th>
966 <th align="left">
967 Purpose
968 </th>
969 </tr>
970 </thead>
971 <tbody valign="top">
972 <tr>
973 <td align="left">
974 <tt>assert_valid(record)</tt>
975 </td>
976 <td align="left">
977 Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.
978 </td>
979 </tr>
980 <tr>
981 <td align="left">
982 <tt>assert_difference(expressions, difference = 1, message = nil) {|| &#8230;}</tt>
983 </td>
984 <td align="left">
985 Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.
986 </td>
987 </tr>
988 <tr>
989 <td align="left">
990 <tt>assert_no_difference(expressions, message = nil, &amp;block)</tt>
991 </td>
992 <td align="left">
993 Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.
994 </td>
995 </tr>
996 <tr>
997 <td align="left">
998 <tt>assert_recognizes(expected_options, path, extras={}, message=nil)</tt>
999 </td>
1000 <td align="left">
1001 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.
1002 </td>
1003 </tr>
1004 <tr>
1005 <td align="left">
1006 <tt>assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)</tt>
1007 </td>
1008 <td align="left">
1009 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.
1010 </td>
1011 </tr>
1012 <tr>
1013 <td align="left">
1014 <tt>assert_response(type, message = nil)</tt>
1015 </td>
1016 <td align="left">
1017 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
1018 </td>
1019 </tr>
1020 <tr>
1021 <td align="left">
1022 <tt>assert_redirected_to(options = {}, message=nil)</tt>
1023 </td>
1024 <td align="left">
1025 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.
1026 </td>
1027 </tr>
1028 <tr>
1029 <td align="left">
1030 <tt>assert_template(expected = nil, message=nil)</tt>
1031 </td>
1032 <td align="left">
1033 Asserts that the request was rendered with the appropriate template file.
1034 </td>
1035 </tr>
1036 </tbody>
1037 </table>
1038 </div>
1039 <div class="para"><p>You'll see the usage of some of these assertions in the next chapter.</p></div>
1040 </div>
1041 <h2 id="_functional_tests_for_your_controllers">4. Functional Tests for Your Controllers</h2>
1042 <div class="sectionbody">
1043 <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>
1044 <h3 id="_what_to_include_in_your_functional_tests">4.1. What to include in your Functional Tests</h3>
1045 <div class="para"><p>You should test for things such as:</p></div>
1046 <div class="ilist"><ul>
1047 <li>
1048 <p>
1049 was the web request successful?
1050 </p>
1051 </li>
1052 <li>
1053 <p>
1054 was the user redirected to the right page?
1055 </p>
1056 </li>
1057 <li>
1058 <p>
1059 was the user successfully authenticated?
1060 </p>
1061 </li>
1062 <li>
1063 <p>
1064 was the correct object stored in the response template?
1065 </p>
1066 </li>
1067 <li>
1068 <p>
1069 was the appropriate message displayed to the user in the view
1070 </p>
1071 </li>
1072 </ul></div>
1073 <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>
1074 <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>
1075 <div class="listingblock">
1076 <div class="content"><!-- Generator: GNU source-highlight 2.9
1077 by Lorenzo Bettini
1078 http://www.lorenzobettini.it
1079 http://www.gnu.org/software/src-highlite -->
1080 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_get_index
1081 get <span style="color: #990000">:</span>index
1082 assert_response <span style="color: #990000">:</span>success
1083 assert_not_nil assigns<span style="color: #990000">(:</span>posts<span style="color: #990000">)</span>
1084 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1085 </tt></pre></div></div>
1086 <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>
1087 <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>
1088 <div class="ilist"><ul>
1089 <li>
1090 <p>
1091 The action of the controller you are requesting. This can be in the form of a string or a symbol.
1092 </p>
1093 </li>
1094 <li>
1095 <p>
1096 An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).
1097 </p>
1098 </li>
1099 <li>
1100 <p>
1101 An optional hash of session variables to pass along with the request.
1102 </p>
1103 </li>
1104 <li>
1105 <p>
1106 An optional hash of flash values.
1107 </p>
1108 </li>
1109 </ul></div>
1110 <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>
1111 <div class="listingblock">
1112 <div class="content"><!-- Generator: GNU source-highlight 2.9
1113 by Lorenzo Bettini
1114 http://www.lorenzobettini.it
1115 http://www.gnu.org/software/src-highlite -->
1116 <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>
1117 </tt></pre></div></div>
1118 <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>
1119 <div class="listingblock">
1120 <div class="content"><!-- Generator: GNU source-highlight 2.9
1121 by Lorenzo Bettini
1122 http://www.lorenzobettini.it
1123 http://www.gnu.org/software/src-highlite -->
1124 <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>
1125 </tt></pre></div></div>
1126 <div class="admonitionblock">
1127 <table><tr>
1128 <td class="icon">
1129 <img src="./images/icons/note.png" alt="Note" />
1130 </td>
1131 <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>
1132 </tr></table>
1133 </div>
1134 <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>
1135 <div class="listingblock">
1136 <div class="content"><!-- Generator: GNU source-highlight 2.9
1137 by Lorenzo Bettini
1138 http://www.lorenzobettini.it
1139 http://www.gnu.org/software/src-highlite -->
1140 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_create_post
1141 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>
1142 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>
1143 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1144
1145 assert_redirected_to post_path<span style="color: #990000">(</span>assigns<span style="color: #990000">(:</span>post<span style="color: #990000">))</span>
1146 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1147 </tt></pre></div></div>
1148 <div class="para"><p>Now you can try running all the tests and they should pass.</p></div>
1149 <h3 id="_available_request_types_for_functional_tests">4.2. Available Request Types for Functional Tests</h3>
1150 <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>
1151 <div class="ilist"><ul>
1152 <li>
1153 <p>
1154 <tt>get</tt>
1155 </p>
1156 </li>
1157 <li>
1158 <p>
1159 <tt>post</tt>
1160 </p>
1161 </li>
1162 <li>
1163 <p>
1164 <tt>put</tt>
1165 </p>
1166 </li>
1167 <li>
1168 <p>
1169 <tt>head</tt>
1170 </p>
1171 </li>
1172 <li>
1173 <p>
1174 <tt>delete</tt>
1175 </p>
1176 </li>
1177 </ul></div>
1178 <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>
1179 <h3 id="_the_4_hashes_of_the_apocalypse">4.3. The 4 Hashes of the Apocalypse</h3>
1180 <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>
1181 <div class="ilist"><ul>
1182 <li>
1183 <p>
1184 <tt>assigns</tt> - Any objects that are stored as instance variables in actions for use in views.
1185 </p>
1186 </li>
1187 <li>
1188 <p>
1189 <tt>cookies</tt> - Any cookies that are set.
1190 </p>
1191 </li>
1192 <li>
1193 <p>
1194 <tt>flash</tt> - Any objects living in the flash.
1195 </p>
1196 </li>
1197 <li>
1198 <p>
1199 <tt>session</tt> - Any object living in session variables.
1200 </p>
1201 </li>
1202 </ul></div>
1203 <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>
1204 <div class="listingblock">
1205 <div class="content"><!-- Generator: GNU source-highlight 2.9
1206 by Lorenzo Bettini
1207 http://www.lorenzobettini.it
1208 http://www.gnu.org/software/src-highlite -->
1209 <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>
1210 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>
1211 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>
1212
1213 <span style="font-style: italic"><span style="color: #9A1900"># Because you can't use assigns[:something] for historical reasons:</span></span>
1214 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>
1215 </tt></pre></div></div>
1216 <h3 id="_instance_variables_available">4.4. Instance Variables Available</h3>
1217 <div class="para"><p>You also have access to three instance variables in your functional tests:</p></div>
1218 <div class="ilist"><ul>
1219 <li>
1220 <p>
1221 <tt>@controller</tt> - The controller processing the request
1222 </p>
1223 </li>
1224 <li>
1225 <p>
1226 <tt>@request</tt> - The request
1227 </p>
1228 </li>
1229 <li>
1230 <p>
1231 <tt>@response</tt> - The response
1232 </p>
1233 </li>
1234 </ul></div>
1235 <h3 id="_a_fuller_functional_test_example">4.5. A Fuller Functional Test Example</h3>
1236 <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>
1237 <div class="listingblock">
1238 <div class="content"><!-- Generator: GNU source-highlight 2.9
1239 by Lorenzo Bettini
1240 http://www.lorenzobettini.it
1241 http://www.gnu.org/software/src-highlite -->
1242 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_create_post
1243 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>
1244 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>
1245 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1246 assert_redirected_to post_path<span style="color: #990000">(</span>assigns<span style="color: #990000">(:</span>post<span style="color: #990000">))</span>
1247 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>
1248 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1249 </tt></pre></div></div>
1250 <h3 id="_testing_views">4.6. Testing Views</h3>
1251 <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>
1252 <div class="admonitionblock">
1253 <table><tr>
1254 <td class="icon">
1255 <img src="./images/icons/note.png" alt="Note" />
1256 </td>
1257 <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>
1258 </tr></table>
1259 </div>
1260 <div class="para"><p>There are two forms of <tt>assert_select</tt>:</p></div>
1261 <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>
1262 <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>
1263 <div class="para"><p>For example, you could verify the contents on the title element in your response with:</p></div>
1264 <div class="listingblock">
1265 <div class="content"><!-- Generator: GNU source-highlight 2.9
1266 by Lorenzo Bettini
1267 http://www.lorenzobettini.it
1268 http://www.gnu.org/software/src-highlite -->
1269 <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>
1270 </tt></pre></div></div>
1271 <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>
1272 <div class="listingblock">
1273 <div class="content"><!-- Generator: GNU source-highlight 2.9
1274 by Lorenzo Bettini
1275 http://www.lorenzobettini.it
1276 http://www.gnu.org/software/src-highlite -->
1277 <pre><tt>assert_select <span style="color: #FF0000">'ul.navigation'</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
1278 assert_select <span style="color: #FF0000">'li.menu_item'</span>
1279 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1280 </tt></pre></div></div>
1281 <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>
1282 <h4 id="_additional_view_based_assertions">4.6.1. Additional View-based Assertions</h4>
1283 <div class="para"><p>There are more assertions that are primarily used in testing views:</p></div>
1284 <div class="tableblock">
1285 <table rules="all"
1286 frame="hsides"
1287 cellspacing="0" cellpadding="4">
1288 <col width="948" />
1289 <col width="640" />
1290 <thead>
1291 <tr>
1292 <th align="left">
1293 Assertion
1294 </th>
1295 <th align="left">
1296 Purpose
1297 </th>
1298 </tr>
1299 </thead>
1300 <tbody valign="top">
1301 <tr>
1302 <td align="left">
1303 <tt>assert_select_email</tt>
1304 </td>
1305 <td align="left">
1306 Allows you to make assertions on the body of an e-mail.
1307 </td>
1308 </tr>
1309 <tr>
1310 <td align="left">
1311 <tt>assert_select_rjs</tt>
1312 </td>
1313 <td align="left">
1314 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.
1315 </td>
1316 </tr>
1317 <tr>
1318 <td align="left">
1319 <tt>assert_select_encoded</tt>
1320 </td>
1321 <td align="left">
1322 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.
1323 </td>
1324 </tr>
1325 <tr>
1326 <td align="left">
1327 <tt>css_select(selector)</tt> or <tt>css_select(element, selector)</tt>
1328 </td>
1329 <td align="left">
1330 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.
1331 </td>
1332 </tr>
1333 </tbody>
1334 </table>
1335 </div>
1336 <div class="para"><p>Here's an example of using <tt>assert_select_email</tt>:</p></div>
1337 <div class="listingblock">
1338 <div class="content"><!-- Generator: GNU source-highlight 2.9
1339 by Lorenzo Bettini
1340 http://www.lorenzobettini.it
1341 http://www.gnu.org/software/src-highlite -->
1342 <pre><tt>assert_select_email <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
1343 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>
1344 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1345 </tt></pre></div></div>
1346 </div>
1347 <h2 id="_integration_testing">5. Integration Testing</h2>
1348 <div class="sectionbody">
1349 <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>
1350 <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>
1351 <div class="listingblock">
1352 <div class="content"><!-- Generator: GNU source-highlight 2.9
1353 by Lorenzo Bettini
1354 http://www.lorenzobettini.it
1355 http://www.gnu.org/software/src-highlite -->
1356 <pre><tt>$ script/generate integration_test user_flows
1357 exists test/integration<span style="color: #990000">/</span>
1358 create test/integration/user_flows_test<span style="color: #990000">.</span>rb
1359 </tt></pre></div></div>
1360 <div class="para"><p>Here's what a freshly-generated integration test looks like:</p></div>
1361 <div class="listingblock">
1362 <div class="content"><!-- Generator: GNU source-highlight 2.9
1363 by Lorenzo Bettini
1364 http://www.lorenzobettini.it
1365 http://www.gnu.org/software/src-highlite -->
1366 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
1367
1368 <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
1369 <span style="font-style: italic"><span style="color: #9A1900"># fixtures :your, :models</span></span>
1370
1371 <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
1372 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_truth
1373 assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
1374 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1375 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1376 </tt></pre></div></div>
1377 <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>
1378 <h3 id="_helpers_available_for_integration_tests">5.1. Helpers Available for Integration tests</h3>
1379 <div class="para"><p>In addition to the standard testing helpers, there are some additional helpers available to integration tests:</p></div>
1380 <div class="tableblock">
1381 <table rules="all"
1382 frame="hsides"
1383 cellspacing="0" cellpadding="4">
1384 <col width="948" />
1385 <col width="640" />
1386 <thead>
1387 <tr>
1388 <th align="left">
1389 Helper
1390 </th>
1391 <th align="left">
1392 Purpose
1393 </th>
1394 </tr>
1395 </thead>
1396 <tbody valign="top">
1397 <tr>
1398 <td align="left">
1399 <tt>https?</tt>
1400 </td>
1401 <td align="left">
1402 Returns <tt>true</tt> if the session is mimicking a secure HTTPS request.
1403 </td>
1404 </tr>
1405 <tr>
1406 <td align="left">
1407 <tt>https!</tt>
1408 </td>
1409 <td align="left">
1410 Allows you to mimic a secure HTTPS request.
1411 </td>
1412 </tr>
1413 <tr>
1414 <td align="left">
1415 <tt>host!</tt>
1416 </td>
1417 <td align="left">
1418 Allows you to set the host name to use in the next request.
1419 </td>
1420 </tr>
1421 <tr>
1422 <td align="left">
1423 <tt>redirect?</tt>
1424 </td>
1425 <td align="left">
1426 Returns <tt>true</tt> if the last request was a redirect.
1427 </td>
1428 </tr>
1429 <tr>
1430 <td align="left">
1431 <tt>follow_redirect!</tt>
1432 </td>
1433 <td align="left">
1434 Follows a single redirect response.
1435 </td>
1436 </tr>
1437 <tr>
1438 <td align="left">
1439 <tt>request_via_redirect(http_method, path, [parameters], [headers])</tt>
1440 </td>
1441 <td align="left">
1442 Allows you to make an HTTP request and follow any subsequent redirects.
1443 </td>
1444 </tr>
1445 <tr>
1446 <td align="left">
1447 <tt>post_via_redirect(path, [parameters], [headers])</tt>
1448 </td>
1449 <td align="left">
1450 Allows you to make an HTTP POST request and follow any subsequent redirects.
1451 </td>
1452 </tr>
1453 <tr>
1454 <td align="left">
1455 <tt>get_via_redirect(path, [parameters], [headers])</tt>
1456 </td>
1457 <td align="left">
1458 Allows you to make an HTTP GET request and follow any subsequent redirects.
1459 </td>
1460 </tr>
1461 <tr>
1462 <td align="left">
1463 <tt>put_via_redirect(path, [parameters], [headers])</tt>
1464 </td>
1465 <td align="left">
1466 Allows you to make an HTTP PUT request and follow any subsequent redirects.
1467 </td>
1468 </tr>
1469 <tr>
1470 <td align="left">
1471 <tt>delete_via_redirect(path, [parameters], [headers])</tt>
1472 </td>
1473 <td align="left">
1474 Allows you to make an HTTP DELETE request and follow any subsequent redirects.
1475 </td>
1476 </tr>
1477 <tr>
1478 <td align="left">
1479 <tt>open_session</tt>
1480 </td>
1481 <td align="left">
1482 Opens a new session instance.
1483 </td>
1484 </tr>
1485 </tbody>
1486 </table>
1487 </div>
1488 <h3 id="_integration_testing_examples">5.2. Integration Testing Examples</h3>
1489 <div class="para"><p>A simple integration test that exercises multiple controllers:</p></div>
1490 <div class="listingblock">
1491 <div class="content"><!-- Generator: GNU source-highlight 2.9
1492 by Lorenzo Bettini
1493 http://www.lorenzobettini.it
1494 http://www.gnu.org/software/src-highlite -->
1495 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
1496
1497 <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
1498 fixtures <span style="color: #990000">:</span>users
1499
1500 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_login_and_browse_site
1501 <span style="font-style: italic"><span style="color: #9A1900"># login via https</span></span>
1502 https!
1503 get <span style="color: #FF0000">"/login"</span>
1504 assert_response <span style="color: #990000">:</span>success
1505
1506 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
1507 assert_equal <span style="color: #FF0000">'/welcome'</span><span style="color: #990000">,</span> path
1508 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>
1509
1510 https!<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">)</span>
1511 get <span style="color: #FF0000">"/posts/all"</span>
1512 assert_response <span style="color: #990000">:</span>success
1513 assert assigns<span style="color: #990000">(:</span>products<span style="color: #990000">)</span>
1514 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1515 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1516 </tt></pre></div></div>
1517 <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>
1518 <div class="para"><p>Here's an example of multiple sessions and custom DSL in an integration test</p></div>
1519 <div class="listingblock">
1520 <div class="content"><!-- Generator: GNU source-highlight 2.9
1521 by Lorenzo Bettini
1522 http://www.lorenzobettini.it
1523 http://www.gnu.org/software/src-highlite -->
1524 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
1525
1526 <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
1527 fixtures <span style="color: #990000">:</span>users
1528
1529 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_login_and_browse_site
1530
1531 <span style="font-style: italic"><span style="color: #9A1900"># User avs logs in</span></span>
1532 avs <span style="color: #990000">=</span> login<span style="color: #990000">(:</span>avs<span style="color: #990000">)</span>
1533 <span style="font-style: italic"><span style="color: #9A1900"># User guest logs in</span></span>
1534 guest <span style="color: #990000">=</span> login<span style="color: #990000">(:</span>guest<span style="color: #990000">)</span>
1535
1536 <span style="font-style: italic"><span style="color: #9A1900"># Both are now available in different sessions</span></span>
1537 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>
1538 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>
1539
1540 <span style="font-style: italic"><span style="color: #9A1900"># User avs can browse site</span></span>
1541 avs<span style="color: #990000">.</span>browses_site
1542 <span style="font-style: italic"><span style="color: #9A1900"># User guest can browse site aswell</span></span>
1543 guest<span style="color: #990000">.</span>browses_site
1544
1545 <span style="font-style: italic"><span style="color: #9A1900"># Continue with other assertions</span></span>
1546 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1547
1548 private
1549
1550 <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> CustomDsl
1551 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> browses_site
1552 get <span style="color: #FF0000">"/products/all"</span>
1553 assert_response <span style="color: #990000">:</span>success
1554 assert assigns<span style="color: #990000">(:</span>products<span style="color: #990000">)</span>
1555 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1556 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1557
1558 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> login<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
1559 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>
1560 sess<span style="color: #990000">.</span>extend<span style="color: #990000">(</span>CustomDsl<span style="color: #990000">)</span>
1561 u <span style="color: #990000">=</span> users<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
1562 sess<span style="color: #990000">.</span>https!
1563 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
1564 assert_equal <span style="color: #FF0000">'/welcome'</span><span style="color: #990000">,</span> path
1565 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>
1566 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1567 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1568 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1569 </tt></pre></div></div>
1570 </div>
1571 <h2 id="_rake_tasks_for_running_your_tests">6. Rake Tasks for Running your Tests</h2>
1572 <div class="sectionbody">
1573 <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>
1574 <div class="para"><p>--------------------------------`----------------------------------------------------
1575 Tasks Description</p></div>
1576 <div class="listingblock">
1577 <div class="content">
1578 <pre><tt>+rake test+ Runs all unit, functional and integration tests. You can also simply run +rake+ as the _test_ target is the default.
1579 +rake test:units+ Runs all the unit tests from +test/unit+
1580 +rake test:functionals+ Runs all the functional tests from +test/functional+
1581 +rake test:integration+ Runs all the integration tests from +test/integration+
1582 +rake test:recent+ Tests recent changes
1583 +rake test:uncommitted+ Runs all the tests which are uncommitted. Only supports Subversion
1584 +rake test:plugins+ Run all the plugin tests from +vendor/plugins/*/**/test+ (or specify with +PLUGIN=_name_+)</tt></pre>
1585 </div></div>
1586 </div>
1587 <h2 id="_brief_note_about_test_unit">7. Brief Note About Test::Unit</h2>
1588 <div class="sectionbody">
1589 <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>
1590 <div class="admonitionblock">
1591 <table><tr>
1592 <td class="icon">
1593 <img src="./images/icons/note.png" alt="Note" />
1594 </td>
1595 <td class="content">For more information on <tt>Test::Unit</tt>, refer to <a href="http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/">test/unit Documentation</a></td>
1596 </tr></table>
1597 </div>
1598 </div>
1599 <h2 id="_setup_and_teardown">8. Setup and Teardown</h2>
1600 <div class="sectionbody">
1601 <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>
1602 <div class="listingblock">
1603 <div class="content"><!-- Generator: GNU source-highlight 2.9
1604 by Lorenzo Bettini
1605 http://www.lorenzobettini.it
1606 http://www.gnu.org/software/src-highlite -->
1607 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
1608
1609 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsControllerTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>TestCase
1610
1611 <span style="font-style: italic"><span style="color: #9A1900"># called before every single test</span></span>
1612 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> setup
1613 <span style="color: #009900">@post</span> <span style="color: #990000">=</span> posts<span style="color: #990000">(:</span>one<span style="color: #990000">)</span>
1614 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1615
1616 <span style="font-style: italic"><span style="color: #9A1900"># called after every single test</span></span>
1617 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> teardown
1618 <span style="font-style: italic"><span style="color: #9A1900"># as we are re-initializing @post before every test</span></span>
1619 <span style="font-style: italic"><span style="color: #9A1900"># setting it to nil here is not essential but I hope</span></span>
1620 <span style="font-style: italic"><span style="color: #9A1900"># you understand how you can use the teardown method</span></span>
1621 <span style="color: #009900">@post</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span>
1622 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1623
1624 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_show_post
1625 get <span style="color: #990000">:</span>show<span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>id
1626 assert_response <span style="color: #990000">:</span>success
1627 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1628
1629 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_destroy_post
1630 assert_difference<span style="color: #990000">(</span><span style="color: #FF0000">'Post.count'</span><span style="color: #990000">,</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
1631 delete <span style="color: #990000">:</span>destroy<span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>id
1632 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1633
1634 assert_redirected_to posts_path
1635 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1636
1637 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1638 </tt></pre></div></div>
1639 <div class="para"><p>Above, the <tt>setup</tt> method is called before each test and so <tt>@post</tt> is available for each of the tests. Rails implements <tt>setup</tt> and <tt>teardown</tt> as ActiveSupport::Callbacks. Which essentially means you need not only use <tt>setup</tt> and <tt>teardown</tt> as methods in your tests. You could specify them by using:</p></div>
1640 <div class="ilist"><ul>
1641 <li>
1642 <p>
1643 a block
1644 </p>
1645 </li>
1646 <li>
1647 <p>
1648 a method (like in the earlier example)
1649 </p>
1650 </li>
1651 <li>
1652 <p>
1653 a method name as a symbol
1654 </p>
1655 </li>
1656 <li>
1657 <p>
1658 a lambda
1659 </p>
1660 </li>
1661 </ul></div>
1662 <div class="para"><p>Let's see the earlier example by specifying <tt>setup</tt> callback by specifying a method name as a symbol:</p></div>
1663 <div class="listingblock">
1664 <div class="content"><!-- Generator: GNU source-highlight 2.9
1665 by Lorenzo Bettini
1666 http://www.lorenzobettini.it
1667 http://www.gnu.org/software/src-highlite -->
1668 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'../test_helper'</span>
1669
1670 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsControllerTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>TestCase
1671
1672 <span style="font-style: italic"><span style="color: #9A1900"># called before every single test</span></span>
1673 setup <span style="color: #990000">:</span>initialize_post
1674
1675 <span style="font-style: italic"><span style="color: #9A1900"># called after every single test</span></span>
1676 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> teardown
1677 <span style="color: #009900">@post</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span>
1678 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1679
1680 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_show_post
1681 get <span style="color: #990000">:</span>show<span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>id
1682 assert_response <span style="color: #990000">:</span>success
1683 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1684
1685 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_update_post
1686 put <span style="color: #990000">:</span>update<span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>id<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: #FF0000">}</span>
1687 assert_redirected_to post_path<span style="color: #990000">(</span>assigns<span style="color: #990000">(:</span>post<span style="color: #990000">))</span>
1688 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1689
1690 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_destroy_post
1691 assert_difference<span style="color: #990000">(</span><span style="color: #FF0000">'Post.count'</span><span style="color: #990000">,</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
1692 delete <span style="color: #990000">:</span>destroy<span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>id
1693 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1694
1695 assert_redirected_to posts_path
1696 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1697
1698 private
1699
1700 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> initialize_post
1701 <span style="color: #009900">@post</span> <span style="color: #990000">=</span> posts<span style="color: #990000">(:</span>one<span style="color: #990000">)</span>
1702 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1703
1704 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1705 </tt></pre></div></div>
1706 </div>
1707 <h2 id="_testing_routes">9. Testing Routes</h2>
1708 <div class="sectionbody">
1709 <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>
1710 <div class="listingblock">
1711 <div class="content"><!-- Generator: GNU source-highlight 2.9
1712 by Lorenzo Bettini
1713 http://www.lorenzobettini.it
1714 http://www.gnu.org/software/src-highlite -->
1715 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_route_to_post
1716 assert_routing <span style="color: #FF0000">'/posts/1'</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"posts"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"show"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"1"</span> <span style="color: #FF0000">}</span>
1717 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1718 </tt></pre></div></div>
1719 </div>
1720 <h2 id="_testing_your_mailers">10. Testing Your Mailers</h2>
1721 <div class="sectionbody">
1722 <div class="para"><p>Testing mailer classes requires some specific tools to do a thorough job.</p></div>
1723 <h3 id="_keeping_the_postman_in_check">10.1. Keeping the Postman in Check</h3>
1724 <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>
1725 <div class="para"><p>The goals of testing your <tt>ActionMailer</tt> classes are to ensure that:</p></div>
1726 <div class="ilist"><ul>
1727 <li>
1728 <p>
1729 emails are being processed (created and sent)
1730 </p>
1731 </li>
1732 <li>
1733 <p>
1734 the email content is correct (subject, sender, body, etc)
1735 </p>
1736 </li>
1737 <li>
1738 <p>
1739 the right emails are being sent at the right times
1740 </p>
1741 </li>
1742 </ul></div>
1743 <h4 id="_from_all_sides">10.1.1. From All Sides</h4>
1744 <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>
1745 <h3 id="_unit_testing">10.2. Unit Testing</h3>
1746 <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>
1747 <h4 id="_revenge_of_the_fixtures">10.2.1. Revenge of the Fixtures</h4>
1748 <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>
1749 <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>
1750 <h4 id="_the_basic_test_case">10.2.2. The Basic Test case</h4>
1751 <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>
1752 <div class="listingblock">
1753 <div class="content"><!-- Generator: GNU source-highlight 2.9
1754 by Lorenzo Bettini
1755 http://www.lorenzobettini.it
1756 http://www.gnu.org/software/src-highlite -->
1757 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
1758
1759 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserMailerTest <span style="color: #990000">&lt;</span> ActionMailer<span style="color: #990000">::</span>TestCase
1760 tests UserMailer
1761 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_invite
1762 <span style="color: #009900">@expected</span><span style="color: #990000">.</span>from <span style="color: #990000">=</span> <span style="color: #FF0000">'me@example.com'</span>
1763 <span style="color: #009900">@expected</span><span style="color: #990000">.</span>to <span style="color: #990000">=</span> <span style="color: #FF0000">'friend@example.com'</span>
1764 <span style="color: #009900">@expected</span><span style="color: #990000">.</span>subject <span style="color: #990000">=</span> <span style="color: #FF0000">"You have been invited by #{@expected.from}"</span>
1765 <span style="color: #009900">@expected</span><span style="color: #990000">.</span>body <span style="color: #990000">=</span> read_fixture<span style="color: #990000">(</span><span style="color: #FF0000">'invite'</span><span style="color: #990000">)</span>
1766 <span style="color: #009900">@expected</span><span style="color: #990000">.</span>date <span style="color: #990000">=</span> Time<span style="color: #990000">.</span>now
1767
1768 assert_equal <span style="color: #009900">@expected</span><span style="color: #990000">.</span>encoded<span style="color: #990000">,</span> UserMailer<span style="color: #990000">.</span>create_invite<span style="color: #990000">(</span><span style="color: #FF0000">'me@example.com'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'friend@example.com'</span><span style="color: #990000">,</span> <span style="color: #009900">@expected</span><span style="color: #990000">.</span>date<span style="color: #990000">).</span>encoded
1769 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1770
1771 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1772 </tt></pre></div></div>
1773 <div class="para"><p>In this test, <tt>@expected</tt> is an instance of <tt>TMail::Mail</tt> that you can use in your tests. It is defined in <tt>ActionMailer::TestCase</tt>. The test above uses <tt>@expected</tt> to construct an email, which it then asserts with email created by the custom mailer. The <tt>invite</tt> fixture is the body of the email and is used as the sample content to assert against. The helper <tt>read_fixture</tt> is used to read in the content from this file.</p></div>
1774 <div class="para"><p>Here's the content of the <tt>invite</tt> fixture:</p></div>
1775 <div class="listingblock">
1776 <div class="content">
1777 <pre><tt>Hi friend@example.com,
1778
1779 You have been invited.
1780
1781 Cheers!</tt></pre>
1782 </div></div>
1783 <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>
1784 <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>
1785 <h3 id="_functional_testing">10.3. Functional Testing</h3>
1786 <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>
1787 <div class="listingblock">
1788 <div class="content"><!-- Generator: GNU source-highlight 2.9
1789 by Lorenzo Bettini
1790 http://www.lorenzobettini.it
1791 http://www.gnu.org/software/src-highlite -->
1792 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
1793
1794 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserControllerTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>TestCase
1795 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_invite_friend
1796 assert_difference <span style="color: #FF0000">'ActionMailer::Base.deliveries.size'</span><span style="color: #990000">,</span> <span style="color: #990000">+</span><span style="color: #993399">1</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
1797 post <span style="color: #990000">:</span>invite_friend<span style="color: #990000">,</span> <span style="color: #990000">:</span>email <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'friend@example.com'</span>
1798 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1799 invite_email <span style="color: #990000">=</span> ActionMailer<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>deliveries<span style="color: #990000">.</span>first
1800
1801 assert_equal invite_email<span style="color: #990000">.</span>subject<span style="color: #990000">,</span> <span style="color: #FF0000">"You have been invited by me@example.com"</span>
1802 assert_equal invite_email<span style="color: #990000">.</span>to<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> <span style="color: #FF0000">'friend@example.com'</span>
1803 assert_match <span style="color: #FF6600">/Hi friend@example.com/</span><span style="color: #990000">,</span> invite_email<span style="color: #990000">.</span>body
1804 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1805 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1806 </tt></pre></div></div>
1807 </div>
1808 <h2 id="_other_testing_approaches">11. Other Testing Approaches</h2>
1809 <div class="sectionbody">
1810 <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>
1811 <div class="ilist"><ul>
1812 <li>
1813 <p>
1814 <a href="http://avdi.org/projects/nulldb/">NullDB</a>, a way to speed up testing by avoiding database use.
1815 </p>
1816 </li>
1817 <li>
1818 <p>
1819 <a href="http://github.com/thoughtbot/factory_girl/tree/master">Factory Girl</a>, as replacement for fixtures.
1820 </p>
1821 </li>
1822 <li>
1823 <p>
1824 <a href="http://www.thoughtbot.com/projects/shoulda">Shoulda</a>, an extension to <tt>test/unit</tt> with additional helpers, macros, and assertions.
1825 </p>
1826 </li>
1827 <li>
1828 <p>
1829 link: <a href="http://rspec.info/">RSpec</a>, a behavior-driven development framework
1830 </p>
1831 </li>
1832 </ul></div>
1833 </div>
1834 <h2 id="_changelog">12. Changelog</h2>
1835 <div class="sectionbody">
1836 <div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/8">Lighthouse ticket</a></p></div>
1837 <div class="ilist"><ul>
1838 <li>
1839 <p>
1840 November 13, 2008: Revised based on feedback from Pratik Naik by <a href="../authors.html#asurve">Akshay Surve</a> (not yet approved for publication)
1841 </p>
1842 </li>
1843 <li>
1844 <p>
1845 October 14, 2008: Edit and formatting pass by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
1846 </p>
1847 </li>
1848 <li>
1849 <p>
1850 October 12, 2008: First draft by <a href="../authors.html#asurve">Akshay Surve</a> (not yet approved for publication)
1851 </p>
1852 </li>
1853 </ul></div>
1854 </div>
1855
1856 </div>
1857 </div>
1858 </body>
1859 </html>