Rails

URLとPATHのちがい

url/pathイメージ
UnsplashRaychanが撮影した写真

10章のテストでrails testに失敗しました。

Running 39 tests in a single process (parallelization threshold is 50)
Run options: --seed 765

# Running:

.......................................

Finished in 0.366858s, 106.3082 runs/s, 256.2299 assertions/s.
39 runs, 94 assertions, 0 failures, 0 errors, 0 skips
$  rails test
Running 39 tests in a single process (parallelization threshold is 50)
Run options: --seed 37023

# Running:

...............................F

Failure:
UsersEditTest#test_successful_edit_with_friendly_forwarding [/Users/xxxxxxxx/environment/sample-app2/test/integration/users_edit_test.rb:41]:
--- expected
+++ actual
@@ -1 +1 @@
-"http://www.example.com/users/762146111/edit"
+"/users/762146111/edit"

rails test test/integration/users_edit_test.rb:39

.......

Finished in 0.462162s, 84.3860 runs/s, 192.5732 assertions/s.
39 runs, 89 assertions, 1 failures, 0 errors, 0 skips

調べてみると、理由はここ。

test "successful edit with friendly forwarding" do
    get edit_user_path(@user)
    assert_equal session[:forwarding_url], edit_user_path(@user)
    log_in_as(@user)
    #assert_redirected_to edit_user_url(@user)
    assert_nil session[:forwarding_url]
    name  = "Foo Bar"
    email = "foo@bar.com"
    patch user_path(@user), params: { user: { name:  name,
                                              email: email,
                                              password:              "",
                                              password_confirmation: "" } }
    assert_not flash.empty?
    assert_redirected_to @user
    @user.reload
    assert_equal name,  @user.name
    assert_equal email, @user.email
  end


edit_user_urlに変更すると成功しました。

rails test
Running 39 tests in a single process (parallelization threshold is 50)
Run options: --seed 47069

# Running:

.......................................

Finished in 0.425811s, 91.5899 runs/s, 223.1037 assertions/s.
39 runs, 95 assertions, 0 failures, 0 errors, 0 skips

pathは相対パスでurlは絶対パス


そのためassert_equalを満たせず失敗していたようです。

参考にいたしました。ありがとうございました。

参考:「Rails」urlとpathの違いを初心者向けに解説

-Rails
-, ,