Skip to content

Instantly share code, notes, and snippets.

@ahmadhasankhan
Last active December 18, 2015 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadhasankhan/32dd76384eed1c4a2590 to your computer and use it in GitHub Desktop.
Save ahmadhasankhan/32dd76384eed1c4a2590 to your computer and use it in GitHub Desktop.
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'
end

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
    DatabaseCleaner.clean_with(:truncation)
  end

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

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

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end
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...
end

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/posts.js.coffee
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