My sinatra application has a security method which runs at the start of some routes. I want to run the same set of authentication rspec tests against each of the routes which I need to be secure, but I don’t want to repeat myself in rspec. How should I do this?
helpers do
def requires_auth!
# stuff
end
end
post '/object' do
requires_auth!
# stuff
end
put '/object' do
requires_auth!
# stuff
end
get '/object' do
# doesn't require auth
# stuff
end
My spec currently looks like this, and looks very repetitive.
describe 'The post request' do
it 'should fail if auth token is invalid'
it 'should fail if auth token has expired'
it 'should pass if <other stuff>'
end
describe 'The put request' do
it 'should fail if auth token is invalid'
it 'should fail if auth token has expired'
it 'should pass if <more other stuff>'
end
describe 'The get request' do
it 'should pass if <other stuff yet again>'
end
I should have RTFM! Looks like this is exactly what
shared_examplesis used for. If you think there’s a better way that’s specific to Sinatra do post away though!