0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsチュートリアル 14.2.6 Ajaxの動作の確認テストについて

Last updated at Posted at 2020-07-03

演習1 で、 format.js をコメントアウトしてもGreenになってしまう。

チュートリアルのテストが十分でない。

  • ユーザのフォロー数 @user.following.count は、確認できている
  • format.js の効果を確認するには、レスポンスのJavascriptを確認する必要がある。
    • follow のテスト: Unfollow がある, followers の値が 1 であること
    • unfollow のテスト: Followがある, followers の値が 0 であること

ついでに、Ajaxでない場合も、レスポンスのフォロー数の統計数値を評価するようにした。

変更したテストのdiffを下記に示します。

diff --git a/test/integration/following_test.rb b/test/integration/following_test.rb
index fdde034..3fb3e5d 100644
--- a/test/integration/following_test.rb
+++ b/test/integration/following_test.rb
@@ -29,12 +29,16 @@ class FollowingTest < ActionDispatch::IntegrationTest
     assert_difference '@user.following.count', 1 do
       post relationships_path, params: { followed_id: @other.id }
     end
+    follow_redirect!
+    assert_select 'strong#followers', text: @other.followers.count.to_s
   end

   test "should follow a user with Ajax" do
     assert_difference '@user.following.count', 1 do
       post relationships_path, xhr: true, params: { followed_id: @other.id }
     end
+    assert_match "Unfollow", response.body
+    assert_match /#followers.*'1'/, response.body
   end

   test "should unfollow a user the standarfd way" do
@@ -43,6 +47,8 @@ class FollowingTest < ActionDispatch::IntegrationTest
     assert_difference '@user.following.count', -1 do
       delete relationship_path(relationship)
     end
+    follow_redirect!
+    assert_select 'strong#followers', text: @other.followers.count.to_s
   end

   test "should unfollow a user with Ajax" do
@@ -51,5 +57,7 @@ class FollowingTest < ActionDispatch::IntegrationTest
     assert_difference '@user.following.count', -1 do
       delete relationship_path(relationship), xhr: true
     end
+    assert_match "Follow", response.body
+    assert_match /#followers.*'0'/, response.body
   end
 end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?