rspec example blocks
I recently discovered an interesting test pattern; defining variables in before and after hooks in the rails helper.
config.before(:example, hook: true) do
@hook = 'before hook'
end
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.
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