2012-08-29 53 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特有的更好的方法尽管发布了!