I recently discovered an interesting test pattern; defining variables in before and after hooks in the rails helper.
rails_helper.rb 1
2
3
config . before ( :example , hook : true ) do
@hook = 'before hook'
end
some_spec.rb 1
2
3
4
5
6
7
8
RSpec . describe "some variable I need" do
describe 'my hook' , hook : true do
it 'runs the hook' do
expect ( @hook ) . to eq 'before hook' # returns true
end
end
end
A Real Example
Let’s say we are testing a controller and want to make sure only an admin has access to specific views.
rails_helper.rb 1
2
3
4
5
6
7
8
9
config . before ( :example , admin_signed_in : true ) do
current_user_double = Test :: User . new :admin => true
allow_any_instance_of ( ApplicationController ) . to receive ( :current_user ) . and_return ( current_user_double )
end
config . before ( :example , user_signed_in : true ) do
current_user_double = Test :: User . new :admin => false
allow_any_instance_of ( ApplicationController ) . to receive ( :current_user ) . and_return ( current_user_double )
end
When setting admin_signed_in to true in a describe block, we have access to an admin user. I think it is cleaner to set up these users in a hook instead of a shared context or explicitly in the spec.
Helpful Links
Hooks