2011-06-10 5 views
1

동일한 컨트롤러에서 다양한 동작을 테스트해야하는 다음과 같은 테스트가 있습니다. 이걸 어떻게 말릴 수 있니? 아래 주석에서 테스트 할 액션에 따라 테스트가 다른 메소드와 액션을 호출해야한다는 것을 알 수 있습니다. 당신은 예제 루프의 어떤 종류를 사용할 수,같은 컨트롤러에서 다른 동작에 의해 공유되는 RSpec 테스트를 마르는 방법

shared_examples_for "preparing for edit partial" do 
    let(:action){ get :new } 

    it "creates a new staff vacation" do 
    StaffVacation.should_receive(:new) 
    action 
    end 

    it "assigns @first_day_of_week" do 
    action 
    assigns(:first_day_of_week).should == 1 
    end 
end 

context 'GET new' do 
    it_should_behave_like 'preparing for edit partial' do 
    let(:action){ get :new } 
    end 
end 

context 'GET edit' do 
    it_should_behave_like 'preparing for edit partial' do 
    let(:action){ get :edit } 
    end 
end 

context 'POST create' do 
    it_should_behave_like 'preparing for edit partial' do 
    let(:action){ post :create } 
    end 
end 

을 또는 :

['get :new', 'get :edit', 'post :create'].each do |action| 
    context action do 
    it "creates a new staff vacation" do 
     StaffVacation.should_receive(:new) 
     eval(action) 
    end 

    it "assigns @first_day_of_week" do 
     eval(action) 
     assigns(:first_day_of_week).should == 1 
    end 
    end 
end 

답변

3

당신이 뭔가를 할 수 있습니다 그 안에 사양을 가지고있는 방법으로

module Auth 
    def unauthorized_redirect(request, action) 
    [nil, :viewer, :editor].each do |a| 
     with_user(a) do 
     eval "#{request} :#{action}" 
     response.should redirect_to login_path 
     # whatever other expectations 
     end 
    end 
    end 
end 
:

include Auth # This is your module with your generalized spec inside a method 

it "redirects without authentication" do 
    unauthorized_redirect("get", "new") 
end 

그런 다음, 우리의 방법에, 우리는 승인의 다른 유형을 통해 루프를 할 수

1

하나의 옵션은 모듈 믹스를 제공 할 수 있습니다

shared_examples_for "preparing for edit partial" do 

    it "creates a new staff vacation" do 
    StaffVacation.should_receive(:new) 
    get :new 
    end 

    it "assigns @first_day_of_week" do 
    get :new 
    assigns(:first_day_of_week).should == 1 
    end 

end 


describe "GET new" do 
    # i want to use 'it_behaves_like "preparing for edit partial"' 
    # and it should use 'get :new' 
end 

describe "GET edit" do 
    # i want to use 'it_behaves_like "preparing for edit partial"' 
    # but it should use 'get :edit' instead 
end 

describe "POST create" do 
    # on unsuccessful save, i want to use 'it_behaves_like "preparing for edit partial"' 
    # but it should use 'post :create' instead 
end 
관련 문제