Practical Web Programming

Wednesday, October 23, 2013

Generate Rails Application with RSpec

Step by step guide:

 1) Generate a new application without the default Test:Unit
$ rails new sample_app --skip-test-unit

2) Add the RSpec gem in the Gemfile
 group :development, :test do
   gem 'rspec-rails', '2.11.0'
      gem 'shoulda-matchers'
 end
 
 # Add PostgreSQL for production (optional).
 group :production do
   gem 'pg', '0.12.2'
 end

3) Include the Capybara gem, which allows us to simulate a user's interaction with the sample application using a natural English-like syntax.
 group :test do
   gem 'capybara', '1.1.2'
 end

4) Install the gems with "--without production" option. This is a remembered option, which means that we don't have to include it in future invocations of Bundler. Instead, we can write simply bundle install and production gems will be ignored automatically.
 $ bundle install --without production

5) Configure Rails to use RSpec (Note the single colon ":").
 $ rails generate rspec:install  
Note: Add --format doc in .rspec file to make the test output hierarchical

 6) Generate a model with
 $ rails generate model Person  # This will create the model class, migration and test

7) Fill-up the migration and run it 9) Run the test. If the following error "Cannot find table 'people'", it's because the test database is not ready.
    $ rake db:test:prepare              # This just ensures that the data model from the development database (db/development.sqlite3) is reflected in the test database (db/test.sqlite3)

0 comments:

Recent Post