2017-02-19 6 views
0

私は通常、手作業によるテストを行っているので、レールアプリケーションをテストするのは初めてです...しかし、私は今度はそれを正しい方法で実行しようとしています。なぜこの単純な最小値は失敗しますか?

なぜこの基本テストに失敗しますか?

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

最初のアサーションは成功しましたが、2番目のアサーションは成功しません。ソースを見るとタイトルが正しいようです。

<title>Home Sign-In</title> 

答えて

2

コントローラメソッドでコールをリダイレクトすると、能動的にレンダリングされません。そのため、assert_selectは使用できません。

次の2つにあなたのテストケースを分割しようとする場合があります。

test "once you go to to app you are asked to sign in" do 
    get "/" 
    assert_redirected_to "https://stackoverflow.com/users/sign_in" 
end 

test "sign in page title is correct" do 
    get "https://stackoverflow.com/users/sign_in" 
    assert_select "title", "Home Sign-In" 
end 
0

@Pavelは正しいです。 リクエストを受け取った後で簡単に応答を確認する方法は、@ response.bodyです。 パベルを示唆しているように だからあなたの場合には

test "once you go to to app you are asked to sign in" do 
    get "/" 
    assert_redirected_to "https://stackoverflow.com/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 

だから、あなたはそれを修正することができ

test "sign in page title is correct" do 
    get "https://stackoverflow.com/users/sign_in" 
    assert_select "title", "Home Sign-In" 
end 
関連する問題