Browse files

Improve the testing guide [ci skip]

Improve the "Testing Rails Applications" guide adding a section for
testing helpers and a little note about associations with ActiveRecord
and YAML fixtures.

Also add a link to Fixture Builder ; yet another replacement for
fixtures such as Machinist or FactoryGirl.
  • Loading branch information...
1 parent e1f4f64 commit 577a6378ba4e2c38f38220c8537ea586997de3ed @robin850 robin850 committed Jun 23, 2013
Showing with 64 additions and 6 deletions.
  1. +64 −6 guides/source/
@@ -66,18 +66,34 @@ Here's a sample YAML fixture file:
# lo & behold! I am a YAML comment!
- name: David Heinemeier Hansson
- birthday: 1979-10-15
- profession: Systems development
+ name: David Heinemeier Hansson
+ birthday: 1979-10-15
+ profession: Systems development
- name: Steve Ross Kellock
- birthday: 1974-09-27
- profession: guy with keyboard
+ name: Steve Ross Kellock
+ birthday: 1974-09-27
+ profession: guy with keyboard
Each fixture is given a name followed by an indented list of colon-separated key/value pairs. Records are typically separated by a blank space. You can place comments in a fixture file by using the # character in the first column. Keys which resemble YAML keywords such as 'yes' and 'no' are quoted so that the YAML Parser correctly interprets them.
+If you are working with [associations](/association_basics.html), you can simply
+define a reference node between two different fixtures. Here's an example with
+a belongs_to/has_many association:
+# In fixtures/categories.yml
+ name: About
+# In fixtures/articles.yml
+ title: Welcome to Rails!
+ body: Hello world!
+ category: about
#### ERB'in It Up
ERB allows you to embed Ruby code within templates. The YAML fixture format is pre-processed with ERB when Rails loads fixtures. This allows you to use Ruby to help you generate some sample data. For example, the following code generates a thousand users:
@@ -990,6 +1006,47 @@ class UserControllerTest < ActionController::TestCase
+Testing helpers
+In order to test helpers, all you need to do is check that the output of the
+helper method matches what you'd expect. Tests related to the helpers are
+located under the `test/helpers` directory. Rails provides a generator which
+generates both the helper and the test file:
+$ rails generate helper User
+ create app/helpers/user_helper.rb
+ invoke test_unit
+ create test/helpers/user_helper_test.rb
+The generated test file contains the following code:
+require 'test_helper'
+class UserHelperTest < ActionView::TestCase
+A helper is just a simple module where you can define methods which are
+available into your views. To test the output of the helper's methods, you just
+have to use a mixin like this:
+class UserHelperTest < ActionView::TestCase
+ include UserHelper
+ test "should return the user name" do
+ # ...
+ end
+Moreover, since the test class extends from `ActionView::TestCase`, you have
+access to Rails' helper methods such as `link_to` or `pluralize`.
Other Testing Approaches
@@ -998,6 +1055,7 @@ The built-in `test/unit` based testing is not the only way to test Rails applica
* [NullDB](, a way to speed up testing by avoiding database use.
* [Factory Girl](, a replacement for fixtures.
* [Machinist](, another replacement for fixtures.
+* [Fixture Builder](, a tool that compiles Ruby factories into fixtures before a test run.
* [MiniTest::Spec Rails](, use the MiniTest::Spec DSL within your rails tests.
* [Shoulda](, an extension to `test/unit` with additional helpers, macros, and assertions.
* [RSpec](, a behavior-driven development framework

0 comments on commit 577a637

Please sign in to comment.