1. On creating project tells Rails not to generate a test directory associated with the default Test::Unit framework. we will be using an alternate testing framework called RSpec to write a thorough test suite.
rails new [project name] --skip-test-unit
2. Add Gems:
Add following gem in development mode so that we have access to RSpec-specific generators, and it includes it in test mode in order to run the tests. We don’t have to install RSpec itself because it is a dependency of rspec-rails and will thus be installed automatically:
group :development, :test do . . gem 'rspec-rails', '2.11.0' . . end
Include the Capybara gem in test mode, which allows us to simulate a user’s interaction with the application using a natural English-like syntax.
group :test do gem 'capybara', '1.1.2' end
To install and include the new gems, we run bundle update and bundle install
bundle update bundle install --without production
3. Next, we need to configure Rails to use RSpec in place of Test::Unit. This can be accomplished with:
rails generate rspec:install
4. Genrate controller with following command to suppress the generation of the default RSpec tests, which we won’t be using. Instead, we’ll create the tests by hand
rails g controller [controller name] --no-test-framework
5. Generate an integration test (request spec) for our abc pages:
rails generate integration_test abc_pages
Above command will create file abc_pages_spec.rb in spec/request directory
6. Write test cases in above created file. (how to write test case have a look at: …)
7. Excute Test cases:
bundle exec rspec spec/requests/abc_pages_spec.rb
to run specific test
bundle exec rspec spec/requests/abc_pages_spec.rb -e "[test case name]"
For automate the running of the tests use ‘Guard’ gem.
You can speed up tests execution by using ‘Spork’ gem.