現在Rspecを使用してDM機能のインスタンス変数のテストをしています。
factoery_botを使用しlet!で擬似的に値を作成しています。比較する値が同じになのでパスするかと思いましたが、テストがうまく通りません。用意した値がおかしいのか、そもそものテストの書き方がおかしいのか、何か知見がありましたらご教授お願いいたします。下記詳細になります。
現在起きているエラー内容
Failures: 1) PostsController#show ログインしている時 @currentUserEntryを割り当てる Failure/Error: end expected #<ActiveRecord::Relation [#<Entry id: 51, user_id: 945, room_id: 52, created_at: "2020-01-09 08:48:25", updated_at: "2020-01-09 08:48:25">]> to match #<Entry id: 51, user_id: 945, room_id: 52, created_at: "2020-01-09 08:48:25", updated_at: "2020-01-09 08:48:25"> Diff: @@ -1,7 +1,7 @@ -#<Entry:0x00007fe32e718fb8 - id: 51, - user_id: 945, - room_id: 52, - created_at: Thu, 09 Jan 2020 17:48:25 JST +09:00, - updated_at: Thu, 09 Jan 2020 17:48:25 JST +09:00> +[#<Entry:0x00007fe32e752a10 + id: 51, + user_id: 945, + room_id: 52, + created_at: Thu, 09 Jan 2020 17:48:25 JST +09:00, + updated_at: Thu, 09 Jan 2020 17:48:25 JST +09:00>] # ./spec/controllers/posts_controller_spec.rb:71:in `block (4 levels) in <top (required)>' 2) PostsController#show ログインしている時 @userEntryを割り当てる Failure/Error: end expected #<ActiveRecord::Relation [#<Entry id: 54, user_id: 946, room_id: 55, created_at: "2020-01-09 08:52:22", updated_at: "2020-01-09 08:52:22">]> to match #<Entry id: 54, user_id: 946, room_id: 55, created_at: "2020-01-09 08:52:22", updated_at: "2020-01-09 08:52:22"> Diff: @@ -1,7 +1,7 @@ -#<Entry:0x00007fe32e693e30 - id: 54, - user_id: 946, - room_id: 55, - created_at: Thu, 09 Jan 2020 17:52:22 JST +09:00, - updated_at: Thu, 09 Jan 2020 17:52:22 JST +09:00> +[#<Entry:0x00007fe32e650798 + id: 54, + user_id: 946, + room_id: 55, + created_at: Thu, 09 Jan 2020 17:52:22 JST +09:00, + updated_at: Thu, 09 Jan 2020 17:52:22 JST +09:00>] # ./spec/controllers/posts_controller_spec.rb:76:in `block (4 levels) in <top (required)>'
アソシエーション
**user.rb** has_many :entries, dependent: :destroy **room.rb** has_many :messages, dependent: :destroy has_many :entries, dependent: :destroy **entry.rb** belongs_to :user belongs_to :room
posts_controller_spec.rbのテスト部分抜粋
**posts_controller_spec.rb** describe '#show' do let(:user) { create(:user) } let!(:post) { create(:post) } let!(:currentUserEntry) { create(:entry, user_id: user.id) } let!(:userEntry) { create(:entry, user_id: post.user.id) } context 'ログインしている時' do before do login user end it "@currentUserEntryを割り当てる" do get :show, params: {id: post.id} expect(assigns(:currentUserEntry)).to match(currentUserEntry) end it "@userEntryを割り当てる" do get :show, params: {id: post.id} expect(assigns(:userEntry)).to match(userEntry) end end
posts_controllerのテストをしたいインスタンス変数
@user = @post.user @currentUserEntry = Entry.where(user_id: current_user.id) @userEntry = Entry.where(user_id: @user.id)
spec/factories/entries.rb
FactoryBot.define do factory :entry do room user end end
spec/factories/rooms.rb
FactoryBot.define do factory :room do end end
spec/factories/uers.rb
FactoryBot.define do factory :user do password = Faker::Internet.password(min_length: 8) nickname {Faker::Name.last_name} email {Faker::Internet.free_email} password {password} password_confirmation {password} address {'東京都'} introduction {'よろしくお願いします'} avatar { Rack::Test::UploadedFile.new(Rails.root.join('spec/support/sample_image.jpg'), 'image/jpeg') } end end
環境
Rails 5.0.7.2
ruby 2.5.1
rspec-rails (3.9.0)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/09 10:16