0

仕様ファイルにこのコンテキストがあります。

  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

2 つの異なるコンテキストで同じコードを繰り返します。この方法のように変換しますが、機能しません。

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

ここでの問題は、パラメーターをメソッド名として使用しなかったことです。

乾式での使用方法を知っていますか?

4

1 に答える 1

0

これ:

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

文字列補間は単一引用符内で評価されないため、機能しません。

二重引用符を使用する必要があります:

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

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

"#{a} #{b}" #=> "alpha beta"
于 2013-06-18T11:28:18.070 に答える