Skip to content

Instantly share code, notes, and snippets.

@spencerldixon
Created August 25, 2020 14:45
Show Gist options
  • Save spencerldixon/19c30bff48ad4377dd98e4f3d2bfe412 to your computer and use it in GitHub Desktop.
Save spencerldixon/19c30bff48ad4377dd98e4f3d2bfe412 to your computer and use it in GitHub Desktop.
RSpec feature tests for simple CRUDing of a resource
require 'rails_helper'
RSpec.feature "Groups", type: :feature do
let(:user) { FactoryBot.create(:user) }
let(:admin) { FactoryBot.create(:user, :with_admin) }
let(:group) { FactoryBot.create(:group) }
let(:groups) { FactoryBot.create_list(:group, 2) }
let(:new_group) { FactoryBot.build(:group) }
scenario 'require authentication' do
visit groups_path
expect(page).to have_content('You need to sign in or sign up before continuing')
end
scenario 'require admin access' do
sign_in user
visit groups_path
expect(current_path).to eq(root_path)
expect(page).to have_content('Only admins can access this page')
end
scenario 'can be viewed' do
groups
sign_in admin
visit groups_path
groups.each do |group|
expect(page).to have_content(group.name)
end
end
scenario 'can be created' do
sign_in admin
expect {
visit new_group_path
fill_in 'Name', with: new_group.name
click_button 'Create Group'
}.to change(Group, :count).by(+1)
expect(page).to have_content('Group was successfully created')
end
scenario 'can be read' do
group
sign_in admin
visit groups_path
first(:link, "Show").click
expect(current_path).to eq(group_path(group))
expect(page).to have_content(group.name)
end
scenario 'can be updated' do
group
old_name = group.name
new_name = 'new_name'
sign_in admin
expect {
visit edit_group_path(group)
fill_in 'Name', with: new_name
click_button 'Update Group'
}.to change { group.reload.name }.from(old_name).to(new_name)
expect(page).to have_content('Group updated successfully')
expect(current_path).to eq(group_path(group))
end
scenario 'can be deleted' do
group
sign_in admin
expect {
visit groups_path
first(:link, "Destroy").click
}.to change(Group, :count).by(-1)
expect(current_path).to eq(groups_path)
expect(page).to have_content('Group destroyed successfully')
expect(page).to_not have_content(group.name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment