2015-11-05 20 views
1

我注意到我一直在對某些用戶禁止的操作重複相同的規範。特別是當是不允許的動作,重定向和閃存應始終是相同的(或多或少),所以我試圖寫一個shared_example這樣的:我正在烘乾我的規格,但在爲控制器測試提出動態請求時遇到問題

shared_examples_for "no access" do |action| 
    it "redirects the user to the homepage" do 
     get action 
     subject.should redirect_to('/') 
    end 

    it "sets a flash message" do 
     get action 
     expect(controller).to set_flash[:alert] 
    end 
    end 

但隨後很快就意識到,獲得不接受動作變量作爲變量。

任何想法解決這個問題,或實現這樣的一些其他方式嗎?

+0

爲什麼你說「不接受作爲變量的動作變量「?你是否收到錯誤,如果有錯,具體是什麼錯誤?您如何在規格中使用共享示例? – eirikir

回答

2

指定共享舉例如下...

shared_examples "no access" do 
    it "redirects the user to the homepage" do 
    action 
    expect(response).to redirect_to('/') 
    end 
end 

然後在你的測試你傳遞action作爲一個讓塊...

it_behaves_like "no access" do 
    let(:action) {get :show, id: my_record.id} 
end 
相關問題