Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Adding Test Suite In Rails -4 with rspec-3
In gem file
group :development, :test do
  gem 'rspec-rails', '~> 3.0.0'
  gem 'factory_girl_rails'
  gem 'capybara'
  gem 'database_cleaner'

Run generators

Next, run the RSpec generator to create the initial skeleton:

rails generate rspec:install
Configure Capybara

To make Capybara available from within RSpec specs, add the following line to spec/rails_helper.rb:

# spec/rails_helper.rb
require 'capybara/rails'

Your Capybara feature specs will also need a home. Add the spec/features/ directory:

$ mkdir spec/features
Configure database_cleaner

Next, make the following adjustments to spec/rails_helper.rb to integrate the database_cleaner gem:

config.use_transactional_fixtures = false
RSpec.configure do |config|

  config.before(:suite) do

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation

  config.before(:each) do

  config.after(:each) do

Include the factory_girl methods

To make the factory_girl gem’s methods (e.g., build and create) easily available in RSpec examples, add this line to the top of your RSpec.configure block in spec/rails_helper.rb:

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods

  # other configurations below...

The .rspec file

By default, RSpec generates a .rspec file in your app’s root. This allows you to set different command line options (see rspec -h) that will be automatically picked up. The default .rspec includes the --warnings option, which can be really noisy even in a brand new Rails app. If you see a lot of warnings later on, you can hide them by removing this line.

$ rails generate resource post title:string content:text published:boolean

If everything’s correctly configured, you should see something close to this:

invoke  active_record
create    db/migrate/20140630160246_create_posts.rb
create    app/models/post.rb
invoke    rspec
create      spec/models/post_spec.rb
invoke      factory_girl
create        spec/factories/posts.rb
invoke  controller
create    app/controllers/posts_controller.rb
invoke    erb
create      app/views/posts
invoke    rspec
create      spec/controllers/posts_controller_spec.rb
invoke    helper
create      app/helpers/posts_helper.rb
invoke      rspec
create        spec/helpers/posts_helper_spec.rb
invoke    assets
invoke      coffee
create        app/assets/javascripts/
invoke      scss
create        app/assets/stylesheets/posts.css.scss
invoke  resource_route
 route    resources :posts

Note the factory at spec/factories/posts.rb, the model spec at specs/models/post_spec.rb, and the controller spec at specs/controllers/post_controller_spec.rb.

With the new specs in place, try running rspec (you can also run rake) to see the results. So far you should only see a few pending specs:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.