我的 sinatra 应用程序有一个在某些路由开始时运行的安全方法。我想针对我需要确保安全的每条路由运行同一组身份验证 rspec 测试,但我不想在 rspec 中重复自己。我该怎么做?
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
我的规范目前看起来像这样,并且看起来非常重复。
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