Install RSpec, Capybara, FactoryBot
Install rspec-rails
https://rubygems.org/gems/rspec-rails
Install this gem inside group :development, :test
NOTE: it must be gem 'rspec-rails' (not gem 'rspec' as it's github shows)
rails generate rspec:install
Generate a stub:
bundle binstubs rspec-core
Enable
--only-failures
by doing the following:In spec/spec_helper.rb, uncomment this line:
config.example_status_persistence_file_path = "spec/examples.txt"
Create the file (leave it blank):
spec/examples.txt
Add
/spec/examples.txt
to .gitignore
Install capybara
Note: If using Jumpstart, capybara is already included
https://rubygems.org/gems/capybara
Install this gem inside group :test
Install FactoryBot
Install FactoryBot:
Rspec Configuration
In spec/rails_helper.rb, inside of Rspec.configure do |config|
... add the following:
config.include Devise::Test::IntegrationHelpers, type: :feature
config.include FactoryBot::Syntax::Methods
Create Factories for user, account, account_user
This assumes you're using a fresh install of Jumpstart.
Create the following files:
spec/factories/account.rb
:
FactoryBot.define do
factory :account, class: Account do
sequence(:name) { |n| "test account #{n}" }
personal { true }
end
end
spec/factories/user.rb
:
FactoryBot.define do
factory :user, class: User do
sequence(:email) { |n| "testuser#{n}@test.com" }
password { "testpassword" }
admin { false }
terms_of_service { true }
sequence(:name) { |n| "test user #{n}" }
end
end
spec/factories/account_user.rb
:
FactoryBot.define do
factory :account_user, class: AccountUser do
user
account
end
end
Create an example spec
Create this in spec/features/example_spec.rb
:
This example assumes you're using a fresh install of Jumpstart. You might need to adjust the content of this spec for it to work.
require "rails_helper"
RSpec.feature "Example spec", js: true, bc10820e: true do
before do
@user = create(:user) # creates account_user, account, user
account_user = @user.account_users.first
@account = @user.accounts.first
sign_in @user
end
scenario "A user can log in and view announcements", bc61520:true do
visit "/"
click_link "What's New"
expect(page).to have_content("Exciting stuff coming very soon!")
end
end
Last updated