Zach Grammon


Developer / Teacher
Share: 

Rails Testing Cheatsheet

Setup

Useful testing gems

group :test do
  gem 'faker'
  gem "capybara"
  gem "selenium-webdriver"
  gem "webdrivers"
  gem 'launchy'
  gem 'shoulda-matchers'
  gem 'rspec-rails'
  gem 'factory_bot_rails', :require => false
end

Rails Config

# config/database.yml
test:
  adapter: postgresql
  database: music_db_test
  pool: 5
  timeout: 5000
# config/environments/test.rb
config.action_mailer.default_url_options = { :host => "localhost:3000" }

Model Testing

Model Testing Config

rails generate rspec:model User

shoulda-matchers

https://github.com/thoughtbot/shoulda-matchers

shoulda-matcher config

# spec/rails_helper.rb
...
RSpec.configure do |config|
    Shoulda::Matchers.configure do |config|
        config.integrate do |with|
            with.test_framework :rspec
            with.library :rails
        end
    end
...

Don’t forget to require it in your class!

# spec/models/user_spec.rb
require 'shoulda/matchers'

FactoryBot & Faker

FactoryBot Config

# spec/rails_helper.rb
# Add additional requires below this line. Rails is not loaded until this point!
require 'support/factory_bot'
# spec/support/factory_bot.rb
require 'factory_bot'

RSpec.configure do |config|
    config.include FactoryBot::Syntax::Methods
end

FactoryBot example definition w/ Faker

FactoryBot.define do
    factory :user do
        email { |n| Faker::Internet.email }
        password { |p| Faker::Internet.password }
    end
end

Model Testing - Example Usage

shoulda-matchers

it { should validate_presence_of(:email) }

FactoryBot in Model

# define user for use inside it blocks
subject(:user) do
    FactoryBot.build(:user)
end
, ,