ROR : TDD / BDD with RSpec : Initial Setup ( Copied )

RSpec Setup in Rails 4

An up-to-date testing environment setup for Rails 4, including RSpec, Capybara, and FactoryGirl.

Gems

Gems gonna used are:

Gems not covered:

Gemfile

Include the following:

group :development do
  gem 'spring'
  gem 'spring-commands-rspec'
end

group :development, :test do
  gem 'rspec-rails', '~> 3.0.0'
  gem 'factory_girl_rails'
end

group :test do
  gem 'database_cleaner'
  gem 'capybara'
  gem 'shoulda-matchers', require: false
end

Setup

  • Initialize RSpec with rails generate rspec:install.
  • Create following directories mkdir spec/factories spec/features spec/support.
  • Create Database Cleaner configuration spec/support/database_cleaner.rb.
RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.clean_with(:deletion)
  end

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

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

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

  config.after(:each) do
    DatabaseCleaner.clean
  end
end
  • Create Factory Girl configuration spec/support/factory_girl.rb.
RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end
  • Require Capybara in spec/rails_helper.rb, after require 'rspec/rails'.
require 'capybara/rails'
require 'shoulda/matchers'
  • Uncomment code in spec/spec_helper.rb.
  • Run bundle binstubs rspec-core to create a binstub for the rspec command so it can be run via bin/rspec.

Commands

RSpec

  • rspec: run all specs.
  • rspec spec/models: run all model specs.
  • rspec spec/models/todo_spec.rb: run specs in a file.

RSpec provides generator rails generate rspec:model widget, which will create a new spec file in spec/models/widget_spec.rb.

Available generators: scaffold, model, controller, helper, view, mailer, observer,integration.

RSpec _spec file example:

require "rails_helper"

RSpec.describe User, :type => :model do
  it "orders by last name" do
    lindeman = User.create!(first_name: "Andy", last_name: "Lindeman")
    chelimsky = User.create!(first_name: "David", last_name: "Chelimsky")

    expect(User.ordered_by_last_name).to eq([chelimsky, lindeman])
  end
end

Additions

Include Routes URL helpers

To use root_path in specs, create spec/support/url_helpers.rb.

RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
end

Include I18n Shorthand

To use t instead of I18n.t in specs, create spec/support/i18n.rb.

RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
end

Include Helpers

Additional helpers can put in spec/support subdirectories.

Additional Knowledge:

Manually generate factories, spec_files, and other

If you have already created the models and controllers before you setup RSpec. No worries, you can manually create necessary files for existing model and controllers.

Sources:

https://semaphoreci.com/blog/2013/08/14/setting-up-bdd-stack-on-a-new-rails-4-application.html

http://www.bicrement.com/articles/2014/rspec-setup-in-rails-4.html

One thought on “ROR : TDD / BDD with RSpec : Initial Setup ( Copied )

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

%d bloggers like this: