2012-08-29 99 views
0

我的sinatra應用程序有一個安全方法,它在某些路由的開始處運行。我想對每個需要安全的路由運行同一組認證rspec測試,但我不想在rspec中重複自己。我應該怎麼做?在sinatra上運行多條路由上的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 

回答

0

我應該有RTFM!看起來這正是shared_examples的用途。如果您認爲Sinatra特有的更好的方法儘管發佈了!