前提・実現したいこと
アプリで予約機能を作っています。
ブラウザでは問題なくできていますが、rspecでテスト失敗で詰まっています。
発生している問題・エラーメッセージ
1) ビデオ通話予約機能 予約登録処理 予約登録は実行できること Failure/Error: expect { post "/reservations", params: { reservation: { day: "2021-07-02", time: "9:00", start_time: "2021-07-02T09:00:00+09:00", user_id: 3 } } }.to change(Reservation, :count).by(1) expected `Reservation.count` to have changed by 1, but was changed by 0 # ./spec/requests/reservations_spec.rb:33:in `block (3 levels) in <main>' # -e:1:in `<main>'
該当のソースコード
spec/requests/reservations_spec.rb
require 'rails_helper' RSpec.describe "ビデオ通話予約機能", type: :request do let(:user) { create(:user) } let(:other_user) { create(:user) } let!(:crop) { create(:crop, user: other_user) } context "予約登録処理" do it "予約登録は実行できること" do login_for_request(user) get "/users/#{other_user.id}/reserve" get new_reservation_path, params: { day: "2021-07-02", time: "9:00" } expect { post "/reservations", params: { reservation: { day: "2021-07-02", time: "9:00", start_time: "2021-07-02T09:00:00+09:00", user_id: 3 } } }.to change(Reservation, :count).by(1) follow_redirect! expect(response).to render_template('reservations/show') end end end
app/models/reservation.rb
class Reservation < ApplicationRecord belongs_to :user belongs_to :crop validates :user_id, presence: true validates :day, presence: true validates :time, presence: true validates :start_time, presence: true, uniqueness: true def self.reservations_after_one_month # 今日から1ヶ月先までのデータを取得 reservations = Reservation.all.where("day >= ?", Date.current).where("day < ?", Date.current >> 1).order(day: :desc) # 配列を作成し、データを格納 # DBアクセスを減らすために必要なデータを配列にデータを入れる reservation_data = [] reservations.each do |reservation| reservations_hash = {} reservations_hash.merge!(day: reservation.day.strftime("%Y-%m-%d"), time: reservation.time) reservation_data.push(reservations_hash) end reservation_data end end
app/controllers/reservations_controller.rb
class ReservationsController < ApplicationController before_action :logged_in_user def new @reservation = Reservation.new @day = params[:day] @time = params[:time] @start_time = DateTime.parse(@day + " " + @time + " " + "JST") end def show @reservation = Reservation.find(params[:id]) end def create @reservation = Reservation.new(reservation_params) if @reservation.save flash[:success] = "ビデオ通話予約しました" redirect_to reservation_path @reservation.id else render :new end end private def reservation_params params.require(:reservation).permit(:day, :time, :user_id, :start_time) end end
ターミナル
Started GET "/reservations/new?day=2021-07-02&time=9%3A00" for ::1 at 2021-06-30 20:03:46 +0900 Processing by ReservationsController#new as HTML Parameters: {"day"=>"2021-07-02", "time"=>"9:00"} User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]] ↳ app/helpers/sessions_helper.rb:43 Rendering reservations/new.html.erb within layouts/application Rendered shared/_error_messages.html.erb (0.4ms) Rendered reservations/new.html.erb within layouts/application (3.4ms) Rendered layouts/_rails_default.html.erb (6.4ms) Rendered layouts/_shim.html.erb (0.3ms) Rendered layouts/_navigation.html.erb (1.1ms) Rendered layouts/_search.html.erb (0.8ms) Rendered layouts/_footer.html.erb (0.2ms) Rendered layouts/_js.html.erb (4.9ms) Completed 200 OK in 35ms (Views: 23.5ms | ActiveRecord: 4.2ms) Started POST "/reservations" for ::1 at 2021-06-30 20:03:47 +0900 Processing by ReservationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"rlG1WUVkXKQwavRqoJto72BhQdrBCEE/X4VxVPOqyVEy6XTXAjLRhr4EVo5ixdHVAls3MD98Phe64kCOu+Pxfg==", "reservation"=>{"day"=>"2021-07-02", "time"=>"9:00", "user_id"=>"3", "start_time"=>"2021-07-02T09:00:00+09:00"}, "commit"=>"登録する"} User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]] ↳ app/helpers/sessions_helper.rb:43 (0.2ms) BEGIN ↳ app/controllers/reservations_controller.rb:17 CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]] ↳ app/controllers/reservations_controller.rb:17 Reservation Exists (0.9ms) SELECT 1 AS one FROM "reservations" WHERE "reservations"."start_time" = $1 LIMIT $2 [["start_time", "2021-07-02 00:00:00"], ["LIMIT", 1]] ↳ app/controllers/reservations_controller.rb:17 Reservation Create (1.0ms) INSERT INTO "reservations" ("user_id", "day", "time", "created_at", "updated_at", "start_time") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["user_id", 3], ["day", "2021-07-02"], ["time", "9:00"], ["created_at", "2021-06-30 11:03:47.069175"], ["updated_at", "2021-06-30 11:03:47.069175"], ["start_time", "2021-07-02 00:00:00"]] ↳ app/controllers/reservations_controller.rb:17 (0.8ms) COMMIT ↳ app/controllers/reservations_controller.rb:17 Redirected to http://localhost:3000/reservations/1 Completed 302 Found in 13ms (ActiveRecord: 3.5ms) Started GET "/reservations/1" for ::1 at 2021-06-30 20:03:47 +0900 Processing by ReservationsController#show as HTML Parameters: {"id"=>"1"} User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]] ↳ app/helpers/sessions_helper.rb:43 Reservation Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] ↳ app/controllers/reservations_controller.rb:12 Rendering reservations/show.html.erb within layouts/application Rendered reservations/show.html.erb within layouts/application (0.5ms) Rendered layouts/_rails_default.html.erb (5.2ms) Rendered layouts/_shim.html.erb (0.3ms) Rendered layouts/_navigation.html.erb (1.2ms) Rendered layouts/_search.html.erb (1.1ms) Rendered layouts/_footer.html.erb (0.3ms) Rendered layouts/_js.html.erb (5.5ms) Completed 200 OK in 25ms (Views: 20.5ms | ActiveRecord: 0.5ms)
補足情報(FW/ツールのバージョンなど)
rubyのバージョンは2.6.7です。
エラー文しか載せていないので必要な箇所があれば載せますのでよろしくお願いします。
model Reservation と controller Reservations のcodeを
回答1件
あなたの回答
tips
プレビュー