2017-10-17 66 views
1

因此,這是使用泊塢窗,它包括我從頭開始建立一些用戶身份驗證一個非常基本的Rails應用程序5(不使用設計等)。現在,我想開始學習關於水豚的請求規格,但是我正在碰到一個看起來很奇怪的問題。要求在水豚一個GET,當它應該是一個POST

這是我的登錄表單(sessions.new.erb):

<%= form_tag sessions_path do %> 
      <form class="m-t" role="form" action="/"> 
       <div class="form-group"> 
       <%= text_field_tag :email, params[:email], class: 'form-control', placeholder: "Email Address", required: "" %> 
       </div> 
       <div class="form-group"> 
       <%= password_field_tag(:password, nil, class: 'form-control', placeholder: "Password", required: "") %> 
       </div> 
       <div class="form-group"> 
         <%= check_box_tag :remember_me, 1, params[:remember_me] %> 
         <%= label_tag :remember_me %> 
         </div> 
       <div class="actions"><%= submit_tag "Log In", class: "btn btn-primary block full-width m-b" %></div> 
      </form> 
     <% end %> 

而且我requests/sessions_spec.rb

require "rails_helper" 

RSpec.feature "Login", :type => :feature do 
    scenario "handles wrong email and password gracefully" do 
    visit login_path 
    fill_in "Email Address", :with => "something" 
    fill_in "Password", :with => "something" 
    click_button "Log In" 

    expect(page).to have_text("Email or password incorrect") 
    end 
end 

現在,這個,如果你手工測試,所以我會假設水豚會看到同樣的事情的作品。但它一直在失敗。我有配置的應用程序,這樣,如果你試圖訪問一個受保護控制器和您沒有登錄,它會將您重定向到/login和閃爍的消息說Please log in to see this page。 Rspec測試返回,這是奇怪的 - 這表明水豚試圖訪問另一頁。

所以我尾的測試日誌(docker-compose run web tail -f log/test.log

而且我發現我百思不得其解:

Started GET "/login" for 127.0.0.1 at 2017-10-17 06:59:26 +0000 
Processing by SessionsController#new as HTML 
    Rendering sessions/new.html.erb within layouts/empty 
    Rendered sessions/new.html.erb within layouts/empty (1.1ms) 
Completed 200 OK in 6ms (Views: 6.1ms | ActiveRecord: 0.0ms) 
Started GET "/?email=something&password=[FILTERED]&commit=Log+In" for 127.0.0.1 at 2017-10-17 06:59:26 +0000 
Started GET "/locations" for 127.0.0.1 at 2017-10-17 06:59:26 +0000 
Processing by LocationsController#index as HTML 
Redirected to http://www.example.com/login 
Filter chain halted as :authenticate rendered or redirected 
Completed 302 Found in 2ms (ActiveRecord: 0.0ms) 
Started GET "/login" for 127.0.0.1 at 2017-10-17 06:59:26 +0000 
Processing by SessionsController#new as HTML 
    Rendering sessions/new.html.erb within layouts/empty 
    Rendered sessions/new.html.erb within layouts/empty (1.1ms) 
Completed 200 OK in 6ms (Views: 4.9ms | ActiveRecord: 0.0ms) 
    (0.4ms) ROLLBACK 

第一位是好的,弄登錄由SessionsController#新的處理。但隨後,(參見第6行)由於某種原因,水豚試圖讓根URL,通過電子郵件/密碼PARAMS英寸我的根URL映射到LocationsController#指數,這對用戶是不允許訪問,因此得到重定向回/登錄消息Please log in to see this page。那個按鈕實際上做的是發送一個POST到SessionsController#create。如果你看日誌,當你做手工,這正是發生了什麼:

web_1 | Started POST "/sessions" for 172.18.0.1 at 2017-10-17 07:02:19+0000 
web_1 | Processing by SessionsController#create as HTML 

我不能,爲什麼在水豚當你按下按鈕,它執行完全不同的請求時,單擊該按鈕摸出手動。

幫助非常感謝!

回答

1

一對夫婦澄清的第一個。

  1. 你不寫請求的規格,你寫的功能規格(通過使用RSpec.feature:type => :feature
  2. 你不需要使用RSpec.feature時,自認爲已經設爲指定:type => :feature證明。

現在既然form_tag在您的視圖代碼添加到您的問題,您有嵌套形式創建<form>元素,然後你直接擁有另一<form>元素內部的(注:它總是更好地發佈實際的HTML而不是erb,所以人們可以看到實際的HTML是什麼)。再加上你似乎是使用rack-test驅動程序(沒有js: true元數據),不會對行爲的方式作爲一個真正的瀏覽器一樣,當HTML是無效的(這嵌套形式)的事實,你結束了你的當前行爲。我猜想,當你用一個真正的瀏覽器使用它時,內部表單元素將被忽略,並且外部表單元素的方法屬性等於「post」,因此它被髮布。當使用rack-test時,它可能會提交沒有method屬性的內部表單元素,因此默認爲「get」。從視圖中刪除多餘的<form class="m-t" role="form" action="/">表單元素,並且事情應該起作用。

+0

呃,完全錯過了嵌套的形式。很明顯,現在你指出了它!非常感謝,也提到了你關於功能和請求規格的觀點。 –

相關問題