Skip to content

Instantly share code, notes, and snippets.

@seanlinsley
Last active October 16, 2015 17:35
Show Gist options
  • Select an option

  • Save seanlinsley/408d29d447e5824a2dcc to your computer and use it in GitHub Desktop.

Select an option

Save seanlinsley/408d29d447e5824a2dcc to your computer and use it in GitHub Desktop.
# When you have really complex scenarios you have to test, your tests can become very long
# and duplicative. Here's a simple way to prevent that:
describe SomeController do
def scenario_setup
post :foo
expect(controller.current_user).to be_a User
# some more complex stuff that takes a lot of space
end
it 'works' do
scenario_setup
get :bar
expect(current_user.role).to eq 'bar'
end
it 'fails without proper authorization' do
scenario_setup
get :baz
expect(response.status).to eq 404
end
# Even better, they can be nested!
context 'with a broken account' do
def even_more_setup
scenario_setup
post :break_my_account
expect(controller.current_user.reload.role).to eq 'what is this???'
end
it 'logs out out and sets a flash error' do
get :dogs
expect(controller.current_user).to eq nil
expect(response.location).to eq '/users/sign_in'
expect(flash[:error]).to eq 'oh no!'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment