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
テーブルやマイグレーションファイルには問題ありませんでした。
どなたかご教授よろしく願います。
回答1件
あなたの回答
tips
プレビュー