Ruby on rails rspec将重复块转换为方法
我的规范文件中有这个上下文Ruby on rails rspec将重复块转换为方法,ruby-on-rails,rspec,Ruby On Rails,Rspec,我的规范文件中有这个上下文 context 'get :index' do it 'should be loaded successfully if current user is not customer' do sign_in @creative get :index response.should be_success end
context 'get :index' do
it 'should be loaded successfully if current user is not customer' do
sign_in @creative
get :index
response.should be_success
end
it 'should redirect to root page if current user is customer' do
sign_in @customer
get :index
response.should redirect_to root_path
end
end
context 'post :create' do
it 'should be loaded successfully if current user is not customer' do
sign_in @creative
post :create
response.should be_success
end
it 'should redirect to root page if current user is customer' do
sign_in @customer
post :create
response.should redirect_to root_path
end
end
我在两个不同的上下文中重复相同的代码。我像这样转换它,但它不起作用
def check_user_sign_in(request_type, action)
context '#{request_type} :#{action}' do
it 'should be loaded successfully if current user is not customer' do
sign_in @creative
request_type action
response.should be_success
end
it 'should redirect to root page if current user is customer' do
sign_in @customer
request_type action
response.should redirect_to root_path
end
end
end
end
这里的问题是我没有使用参数作为方法名
你知道如何在干法中使用它吗?这个:
context '#{request_type} :#{action}' do
将不起作用,因为字符串插值未在单引号内计算
您必须使用双引号:
a = 'alpha' #=> "alpha"
b = 'beta' #=> "beta"
'#{a} #{b}' #=> "\#{a} \#{b}"
"#{a} #{b}" #=> "alpha beta"
谢谢。这对于上下文来说是正确的,但是使用参数作为像请求类型动作这样的方法怎么样。