簡體   English   中英

為什么這個簡單的minitest失敗了?

[英]Why is this simple minitest failing?

我是測試Rails應用程序的新手,因為我通常只是做手動測試...但是這次我正嘗試以正確的方式來做。

為什么此基本測試失敗?

test "once you go to to app you are asked to sign in" do
  get "/"
  assert_redirected_to "/users/sign_in"
  assert_select "title", "Home Sign-In"
end

第一個斷言是成功的,但第二個則不成功。 當我查看源代碼時,標題似乎正確。

<title>Home Sign-In</title>

如果您的控制器方法中有重定向調用,則它不是實際呈現的。 這就是為什么您不能使用assert_select的原因。

您可以嘗試將測試用例分為兩部分:

test "once you go to to app you are asked to sign in" do
  get "/"
  assert_redirected_to "/users/sign_in"
end

test "sign in page title is correct" do
  get "/users/sign_in"
  assert_select "title", "Home Sign-In"
end

@Pavel是正確的。 獲取請求后檢查響應的簡單方法是@ response.body 所以你的情況

test "once you go to to app you are asked to sign in" do
  get "/"
  assert_redirected_to "/users/sign_in"
  byebug #print @response.body here. At this point @response.body will
  # be "You are being redirected to /users/sign_in".
  assert_select "title", "Home Sign-In"
 end

因此您可以按照Pavel的建議進行修改

test "sign in page title is correct" do
  get "/users/sign_in"
  assert_select "title", "Home Sign-In"
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM