人生楽しんで反撃だ!

日々プログラミングで学んだことをアウトプットしたり、人生楽しむための考察をしたりします。

expected の[]の中身がない!

expected のの中身がない!

 

エラー文

1) Item アイテムの保存 アイテムが保存できない場合 ・・・imageが空では登録できない
Failure/Error: expect(@item.errors.full_messages).to include("Content can't be blank")
expected to include "Content can't be blank"
# ./spec/models/item_spec.rb:19:in `block (4 levels) in <top (required)>'

 

 

初歩中の初歩orz

バリデーションがありませんでした。

 

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 = ""
@item.valid?
expect(@item.errors.full_messages).to include("Name can't be blank")
end

it "商品の説明がないとアイテムは保存できない" do
@item.text = ""
@item.valid?
expect(@item.errors.full_messages).to include("Text can't be blank")
end

it "カテゴリーの情報がないとアイテムは保存できない" do
@item.category_id = nil
@item.valid?
expect(@item.errors.full_messages).to include("Category is not a number")
end
it "・・・カテゴリーの情報は1以外でないと登録できない" do
@item.category_id = 1
@item.valid?
expect(@item.errors.full_messages).to include("Category must be other than 1")
end



it "商品の状態の情報がないとアイテムは保存できない" do
@item.condition_id = nil
@item.valid?
expect(@item.errors.full_messages).to include("Condition is not a number")
end



it "・・・商品の状態の情報は1以外でないと登録できない" do
@item.condition_id = 1
@item.valid?
expect(@item.errors.full_messages).to include("Condition must be other than 1")
end
 
it "配送料の負担の情報がないとアイテムは保存できない" do
@item.pay_id = nil
@item.valid?
expect(@item.errors.full_messages).to include("Pay is not a number")
end
it "・・・配送料の負担の情報は1以外でないと登録できない" do
@item.pay_id = 1
@item.valid?
expect(@item.errors.full_messages).to include("Pay must be other than 1")
end
it "発送元の地域の情報がないとアイテムは保存できない" do
@item.area_id = nil
@item.valid?
expect(@item.errors.full_messages).to include("Area is not a number")
end
it "・・・発送元の地域の情報は1以外でないと登録できない" do
@item.area_id = 1
@item.valid?
expect(@item.errors.full_messages).to include("Area must be other than 1")
end

it "発送までの日数の情報がないとアイテムは保存できない" do
@item.day_id = nil
@item.valid?
expect(@item.errors.full_messages).to include("Day is not a number")
end
it "・・・発送までの日数は1以外でないと登録できない" do
@item.day_id = 1
@item.valid?
expect(@item.errors.full_messages).to include("Day must be other than 1")
end

it "価格の情報がないとアイテムは保存できない" do
@item.price = ""
@item.valid?
expect(@item.errors.full_messages).to include("Price is not a number")
end
it "価格が299円以下だと出品できない" do
@item.price = 1
@item.valid?
expect(@item.errors.full_messages).to include("Price must be greater than or equal to 300")
end
it "価格が10000000円以上だと出品できない" do
@item.price = 10000001
@item.valid?
expect(@item.errors.full_messages).to include("Price must be less than or equal to 9999999")
end
it "価格が全角文字では登録できないこと" do
@item.price = "てすと"
@item.valid?
expect(@item.errors.full_messages).to include("Price is not a number")
end
it "価格が半角英語だけでは登録できないこと" do
@item.price = "てすと"
@item.valid?
expect(@item.errors.full_messages).to include("Price is not a number")
end
it "価格が半角英数混合では登録できないこと" do
@item.price = "てすと"
@item.valid?
expect(@item.errors.full_messages).to include("Price is not a number")
end
end
end
end

 

app/models/item.rb

class Item < ApplicationRecord
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to :user
has_one_attached :image
belongs_to :category
belongs_to :condition
belongs_to :pay
belongs_to :area
belongs_to :day
 
with_options presence: true do
validates :name
validates :text
validates :image ←バリデーションを追加
end

with_options numericality: { other_than: 1 }do
validates :category_id
validates :condition_id
validates :pay_id
validates :area_id
validates :day_id
end

validates :price, numericality: {only_integer: true, greater_than_or_equal_to: 300, less_than_or_equal_to: 9999999}
end

 

エラー文

1) Item アイテムの保存 アイテムが保存できない場合 ・・・imageが空では登録できない
Failure/Error: expect(@item.errors.full_messages).to include("Content can't be blank")
expected ["Image can't be blank"] to include "Content can't be blank"
# ./spec/models/item_spec.rb:19:in `block (4 levels) in <top (required)>'

 

ちゃんと expected ["Image can't be blank"] って言ってくれました!!