Learning Rails by Example – Chapter 3

Hi friends,

Today i finished third part of the screen-cast and Rails Tutorial book

That chapter fully coved “How to write test case and how to test the project?

I learnt how to create a rails application without creating a test unit and then later use rspec to test the application. I also learnt to generate a controller and to use rspec for testing

In this part we have to change the following file,

  • routers.rb
  • pages_controller.rb
  • pages_controller_spec.rb and have to create a “about.html.erb

Here “page” is the table name. (Ex: $ rails generate controller Page home contact )

At first, when we create a rails project using, “rails new project_name” we have to add a option, which is “-T”.  Here the -T option to the rails command tells Rails not to generate a test directory associated with the default Test::Unit framework


$ rails new third_part -T

Now modify the Gemfile like as the following using your favourite editor

gem 'rails', '3.0.4'
gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3'

group :development do
 gem 'rspec-rails', '2.5.0'

group :test do
 gem 'rspec', '2.5.0'
 gem 'webrat', '0.7.1'




Now, we have to install all bundles using,

$ bundle install

Then type, to create an rspec configuration
$ rails generate rspec:install
You can also install spork using gem install.
$ sudo gem install spork
Now run,
$ spork –bootstrap

Then, create a pages controller, using “generate” command

$ rails generate controller Pages home contact
create  app/controllers/pages_controller.rb
route  get "pages/contact"
route  get "pages/home"
invoke  erb
create    app/views/pages
create    app/views/pages/home.html.erb
create    app/views/pages/contact.html.erb
invoke  rspec
create    spec/controllers/pages_controller_spec.rb
create    spec/views/pages
create    spec/views/pages/home.html.erb_spec.rb
create    spec/views/pages/contact.html.erb_spec.rb
invoke  helper
create    app/helpers/pages_helper.rb
invoke    rspe

Testing Tools:

the followings are testing tools, now we must install thus Gems

$ gem install autotest -v 4.4.6
$ gem install autotest-rails-pure -v 4.1.2
$ gem install autotest-fsevent -v 0.2.4
$ gem install autotest-growl -v 0.2.9
$ gem install autotest-inotify

Create a file “.autotest” in the project directory and add these lines

require ‘autotest/growl’
require ‘autotest/inotify’
Now to run the test using rspec,

$ rspec spec
If the test is passed, the next line will be lit up in green, otherwise it’ll be in red with the errors.

Now run spork in a new terminal, to load all the necessary dependencies so that we don’t need to load it everytime.

$ spork

“ll continue 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s