2013-12-15 66 views
1

嘗試在登錄用戶嘗試訪問新建並創建操作後,在頁面重定向後測試通知。使用Rspec在頁面重定向後測試通知

規格故障:

失敗:

1) Authentication authorization as signed in master submitting a GET request to the Masters#new action 
    Failure/Error: it { should have_content("You cannot complete this request when signed in.") } 
    Capybara::ElementNotFound: 
     Unable to find xpath "/html" 
    # ./spec/requests/authentication_pages_spec.rb:126:in `block (5 levels) in <top (required)>' 

    2) Authentication authorization as signed in master submitting a POST request to the Masters#create action 
    Failure/Error: it { should have_content("You cannot complete this request when signed in.") } 
    Capybara::ElementNotFound: 
     Unable to find xpath "/html" 
    # ./spec/requests/authentication_pages_spec.rb:132:in `block (5 levels) in <top (required)>' 

過濾器之前:

before_action :already_signed_in, only: [:new, :create] 

def already_signed_in 
    redirect_to root_url, notice: "You cannot complete this request when signed in." if signed_in? 
end 

授權規格:

context "as signed in master" do 
    let(:master) { FactoryGirl.create(:master) } 
    before { sign_in master, no_capybara: true } 
    subject { page } 

    context "submitting a GET request to the Masters#new action" do 
    before { get new_master_path } 
    specify { expect(response).to redirect_to(root_url) } 
    it { should have_content("You cannot complete this request when signed in.") } 
    end 

    context "submitting a POST request to the Masters#create action" do 
    before { post masters_path } 
    specify { expect(response).to redirect_to(root_url) } 
    it { should have_content("You cannot complete this request when signed in.") } 
    end 
end 

回答

3

嗯,是的!該響應沒有任何內容,因爲它是重定向!

因此,您可以詢問flash對象是否有消息。

如果您確實想知道是否在下一頁顯示該內容,您需要編寫集成測試,因爲它實際上會跟隨瀏覽器中的重定向並檢查後續頁面中的消息。

+0

好吧謝謝!我做了flash [:notice]這個主題,然後寫了它{should =〜/當你登錄/ i時你不能完成這個請求} –