Railsでテスト

参考にしたもの

Rails テスティングガイド - Railsガイド

詰まった事

ユーザーのモデルテストでfollowのテストを下記の様に書き、followingをどう書こうか悩んだ。

  • 修正前
test "follow" do
   me = users(:alice)
   other_user = users(:bob)
   assert_not me.following?(other_user)
   me.follow(other_user)
   assert me.following?(other_user)
 end

でもこの中にfollowing?入っちゃってるし、これもしfollowing?メソッドが間違えていたらこのテスト自体が意味ないどころかややこしくなってしまうのでは😵と訳が分からなくなる。

1つのテストにはメソッドは1つにしないと危険だと思われる。

  • 修正後
test "follow" do
   me = users(:alice)
   other_user = users(:bob)
   assert_difference 'other_user.followers.count', 1 do
     me.follow(other_user)
   end
 end