0

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

1 回答 1

0

我应该有RTFM!看起来这正是shared_examples它的用途。如果您认为有特定于 Sinatra 的更好方法,请务必发布!

于 2012-08-29T13:55:37.980 回答