質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

4882閲覧

商品情報入力がうまくいくときのテストコードがエラーを起こす

murohi-08

総合スコア12

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/12/07 10:02

furimaの商品情報の入力で失敗したときのテストコードはうまくいくのですが、成功したときのテストがうまくいきません。
以下がエラーコードです。

Failure/Error: expect(@item).to be_valid expected #<Item id: nil, name: "名前", introduction: "説明", price: 2000, item_condition_id: 1, postage_id: 1, prefecture_id: 1, prepare_id: 1, category_id: 1, user_id: nil, created_at: nil, updated_at: nil> to be valid, but got errors: User must exist, Image can't be blank, Item condition can't be blank, Postage can't be blank, Prefecture can't be blank, Prepare can't be blank, Category can't be blank

以下ファイルになります。

spec/factories/items.rb FactoryBot.define do factory :item do name {"名前"} introduction {"説明"} price {2000} item_condition_id {1} postage_id {1} prefecture_id {1} prepare_id {1} category_id {1} end end
spec/models/item_spec.rb require 'rails_helper' RSpec.describe Item, type: :model do before do @item = FactoryBot.build(:item) end describe '商品情報入力' do context '商品情報入力がうまくいく時' do it '全ての値が正しく入力されていれば出品できること' do #この部分 expect(@item).to be_valid end end context '商品情報の入力がうまくいかない時' do it 'imageが空だと出品できない' do @item.image = nil @item.valid? expect(@item.errors.full_messages).to include("Image can't be blank") end it 'nameが空だと出品できない' do @item.name = nil @item.valid? expect(@item.errors.full_messages).to include("Name can't be blank") end it 'introductionが空だと出品できない' do @item.introduction = nil @item.valid? expect(@item.errors.full_messages).to include("Introduction can't be blank") end it 'categoryが未選択だと出品できない' do @item.category_id = 0 @item.valid? expect(@item.errors.full_messages).to include("Category can't be blank") end it 'item_conditionが未選択だと出品できない' do @item.item_condition_id = 0 @item.valid? expect(@item.errors.full_messages).to include("Item condition can't be blank") end it 'postageが未選択だと出品できない' do @item.postage_id = 0 @item.valid? expect(@item.errors.full_messages).to include("Postage can't be blank") end it 'prefectureが未選択だと出品できない' do @item.prefecture_id = 0 @item.valid? expect(@item.errors.full_messages).to include("Prefecture can't be blank") end it 'prepareが未選択だと出品できない' do @item.prepare_id = 0 @item.valid? expect(@item.errors.full_messages).to include("Prepare can't be blank") end it 'priceが空だと出品できない' do @item.price = nil @item.valid? expect(@item.errors.full_messages).to include("Price can't be blank") end it 'priceが全角数字だと出品できない' do @item.price = "2000" @item.valid? expect(@item.errors.full_messages).to include("Price Half-width number.") end it 'priceが設定範囲以外だと出品できない' do @item.price = 100000000 @item.valid? expect(@item.errors.full_messages).to include("Price Out of setting range") end end end end
app/models/iten.rb class Item < ApplicationRecord belongs_to :user has_one :purchase_history has_one_attached :image extend ActiveHash::Associations::ActiveRecordExtensions belongs_to_active_hash :prefecture belongs_to_active_hash :prepare belongs_to_active_hash :postage belongs_to_active_hash :item_condition belongs_to_active_hash :category with_options presence: true do validates :image validates :name validates :introduction validates :price validates :price, numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9999999, message: "Out of setting range"} validates :price, numericality: { only_integer: true, message: "Half-width number." } validates :item_condition, numericality: { other_than: 0, message: "can't be blank" } validates :postage, numericality: { other_than: 0, message: "can't be blank" } validates :prefecture, numericality: { other_than: 0, message: "can't be blank" } validates :prepare, numericality: { other_than: 0, message: "can't be blank" } validates :category, numericality: { other_than: 0, message: "can't be blank" } end end

テーブルやマイグレーションファイルには問題ありませんでした。
どなたかご教授よろしく願います。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2020/12/07 10:06

but got errors 以降に原因書いてありますが、読まれましたでしょうか。
m.ts10806

2020/12/07 10:49

原因と言うか、自身で書いたテストコードのメッセージが出てるだけでは・・
murohi-08

2020/12/07 11:37

ご回答ありがとうございます。 User must existと言うところでしょうか?before doの中に@userのインスタンス変数にFactoryBotを代入する形でやったほうがいいですかね?
guest

回答1

0

ベストアンサー

そのメッセージに出ているではないですか
User must exist, Image can't be blank, Item condition can't be blank, Postage can't be blank, Prefecture can't be blank, Prepare can't be blank, Category can't be blan
image condition postage prefecture prepare category が必須だと言ってます
factoryに
postage_id {1} prefecture_id {1} prepare_id {1} category_id
という形で入っているかにみえますが
postage,prefecture, prepare,category の validationが書かれてますがこれが蛇足です。belongs_toがあるので不要です。
image conditionはBotにないですね。
成功が成功していないと、失敗が正しく失敗かどうか分からないので要注意。

投稿2020/12/08 00:45

winterboum

総合スコア23347

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問