简体   繁体   中英

Why is this simple minitest failing?

I'm new to testing rails applications as I usually just do manual testing... but I'm trying to do it the right way this time.

Why is this basic test failing?

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

The first assertion is successful but not the second. The title seems correct when I view source.

<title>Home Sign-In</title>

If you have redirect call in your controller method it is not acturally rendered. That's why you can't use assert_select .

You may try to divide your test case into two:

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 is right. A simple way to check the response after get request is @response.body . So in your case

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

So you can modify it as Pavel has suggested

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM