使用sinatra在多个路由上运行相同的rspec测试

时间:2012-08-29 13:23:55

标签: rspec sinatra dry

我的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

1 个答案:

答案 0 :(得分:0)

我应该有RTFM!看起来这正是shared_examples的用途。如果你认为有一种更好的方式,特别是Sinatra会发布消息!