はじめに
本記事では、いいね機能のテストについて記述します。
いいね機能のテストコードを書いておりました。
前提
- いいね機能は導入済みです。
- 私が、実装している
いいね機能
は、投稿した食べ物(food)
をユーザーが(user)
がいいねする
ものになっています。
コード
早速ですが、コードを記述します。
初めの方のsleep 0.2
は必要ありません。
自宅のWi-Fiがとんでもないぐらい遅いので記述しています。一応記事を添付します。
require 'rails_helper'
RSpec.describe Like, type: :model do
before do
user = FactoryBot.create(:user)
food = FactoryBot.create(:food)
@like = FactoryBot.build(:like, user_id: user.id, food_id: food.id)
sleep 0.2
end
describe 'いいね機能' do
context 'いいねできる場合' do
it "user_idとfood_idがあれば保存できる" do
expect(@like).to be_valid
end
it "food_idが同じでもuser_idが違えばいいねできる" do
another_like = FactoryBot.create(:like)
expect(FactoryBot.create(:like, user_id: another_like.user_id)).to be_valid
end
it "user_idが同じでもfood_idが違えばいいねできる" do
another_like = FactoryBot.create(:like)
expect(FactoryBot.create(:like, food_id: another_like.food_id)).to be_valid
end
end
context 'いいねできない場合' do
it "user_idが空ではいいねできない" do
@like.user_id = nil
@like.valid?
expect(@like.errors.full_messages).to include "User must exist"
end
it "food_idが空ではいいねできない" do
@like.food_id = nil
@like.valid?
expect(@like.errors.full_messages).to include "Food must exist"
end
end
end
end
テストする項目として、
- user_idとfood_idがあれば保存できる
- food_idが同じでもuser_idが違えばいいねできる
- user_idが同じでもfood_idが違えばいいねできる
- user_idが空ではいいねできない
- food_idが空ではいいねできない
の5つかなと考えました。
context 'いいねできない場合' do
it "user_idが空ではいいねできない" do
@like.user_id = nil
@like.valid?
bindng.pry
expect(@like.errors.full_messages).to include "User must exist"
end
it "food_idが空ではいいねできない" do
@like.food_id = nil
@like.valid?
expect(@like.errors.full_messages).to include "Food must exist"
end
end
ここに関しては、
なかなかうまくいかなかったので、@like.valid?
の後にbinding.pry
して、
何がOKで、何がダメなのかを考えていきました。
27:
28: context 'いいねできない場合' do
29: it "user_idが空ではいいねできない" do
30: @like.user_id = nil
31: @like.valid?
=> 32: binding.pry
33: expect(@like.errors.full_messages).to include "User must exist"
34: end
35:
36: it "food_idが空ではいいねできない" do
37: @like.food_id = nil
[1] pry(#<RSpec::ExampleGroups::Like::Nested::Nested_2>)>
@like.errors.full_messages
について、
こちらの記事を参考にして、
【Rails6】 RSpecによるLikeのモデル単体テストの実装
@like.errors[:user_id]
と記述しておりましたが、
なかなかうまくいきませんでした。
@like.errors.full_messagesの中身を確かめたところ、
無事に["User must exist"]
が出現しました。
27:
28: context 'いいねできない場合' do
29: it "user_idが空ではいいねできない" do
30: @like.user_id = nil
31: @like.valid?
=> 32: binding.pry
33: expect(@like.errors.full_messages).to include "User must exist"
34: end
35:
36: it "food_idが空ではいいねできない" do
37: @like.food_id = nil
[1] pry(#<RSpec::ExampleGroups::Like::Nested::Nested_2>)> @like.valid?
=> false
[2] pry(#<RSpec::ExampleGroups::Like::Nested::Nested_2>)> @like.errors[:user_id]
=> []
[3] pry(#<RSpec::ExampleGroups::Like::Nested::Nested_2>)> @like.errors.full_messages
=> ["User must exist"]
[4] pry(#<RSpec::ExampleGroups::Like::Nested::Nested_2>)>
以上です。
この件について、
私の探し方が悪いのか、記事があまりなさそうでしたので、参考にしていただけますと幸いです。
終わりに
テストコード、うまくいくと結構楽しいですね。
私は、好きです。
また、テストコードを書き始めたとき、以下のようなエラーが起きてました。
Failure/Error: expect(@like.errors.full_massages).to include "User must exist"
NoMethodError:
undefined method `full_massages' for #<ActiveModel::Errors:0x00007fb04cb95d30>
Did you mean? full_messages
full_message
「なんでや?あってるやんけ!」と30分調べた末、
massages
になってました...。
「マッサーゲっておま。。。」
タイポはほんと注意しないといけないですね。。
以下参考記事です。
【Rails6】 RSpecによるLikeのモデル単体テストの実装
Railsでいいね機能(ajax処理)のRSpecの書き方
今日中に、フォロー機能とコメント機能のテストも完了させたいと思います!
今日の残りと明日も頑張りましょう!