0

このコントローラースペックファイルがあります。

describe BriefNotesController do
  render_views

  before(:all) do
    @customer=Factory(:customer)
    @project=Factory(:project_started, :owner => @customer)
  end

  context 'get :new' do

    it 'should redirect to login page for not signed in users' do
      get :new, :project_id => @project.id
      response.should new_user_session_path
    end


  end

  context 'get :create' do

    it 'should redirect to login page for not signed in users' do
      post :create, {:project_id => @project.to_param, :brief_note => Factory.attributes_for(:brief_note, :project_id => @project.id)}
      response.should redirect_to new_user_session_path
    end
end

これらのテストでは、アクションごとに同じ認証テストを繰り返すため、ドライな方法で再コーディングしたいと考えてshared_examples_forいます。メソッドでこのテストを再コーディングしようとしましたが、処理しませんでした。

4

1 に答える 1

0

このようなものはうまくいくことができます

shared_examples_for 'redirects to login page if not signed in' do

  context 'get :new' do
    it 'should redirect to login page for not signed in users' do
      get :new, :project_id => resource.id
      response.should redirect_to new_user_session_path
    end
  end

  context 'get :create' do
    it 'should redirect to login page for not signed in users' do
      post :create, resource_params
      response.should redirect_to new_user_session_path
    end
  end
end

次のように使用します。

describe BriefNoteController do
  let(:customer) { Factory(:customer) }
  let(:project) { Factory(:project_started, :owner => customer) }

  let(:resource) { project }
  let(:resource_params) do
    {
      :project_id => project.to_param,
      :brief_note => Factory.attributes_for(:brief_note, :project_id => project.id)
    }
  end

  it_behaves_like 'redirects to login page if not signed in'
end
于 2013-06-25T12:48:57.243 に答える