2013-06-18 27 views
0

我在我的spec文件中有这个上下文。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 
      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 

我重复相同的代码在两个不同的context.I将其转换这样的方法,但它不工作。

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 

这里的问题是我没有使用参数作为方法的名称。

你知道我怎么用干的方式使用它?

回答

0

此:

context '#{request_type} :#{action}' do 

不会起作用,因为串插不单引号内评估。

必须使用双引号

a = 'alpha' #=> "alpha" 
b = 'beta' #=> "beta" 

'#{a} #{b}' #=> "\#{a} \#{b}" 

"#{a} #{b}" #=> "alpha beta" 
+0

Thanks.It的正确的上下文,但有关使用参数像request_type动作的方法是什么。 –