0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RSpec::Expectations::ExpectationNotMetError:

Posted at

はじめに

rspecでのテスト中にRSpec::Expectations::ExpectationNotMetError: のエラーが
発生しました。
めちゃくちゃ凡ミスでした。二度としないように備忘録に残しておきます!!!

RspecFactoryBotは導入済みの状態で始まります。

エラー紹介

bundle exec rspec spec/models/item_spec.rb

実行時に

RSpec::Expectations::ExpectationNotMetError: expected #<Item id: nil, title: "商品名", price: 50000, concept: "説明文", category_id: 1, condition_id: 1, postage_payer_id: 1, ship_area_id: 1, ship_date_id: 1, user_id: nil, created_at: nil, updated_at: nil> to be valid, but got errors: Category can't be blank, Condition can't be blank, Postage payer can't be blank, Ship area can't be blank, Ship date can't be blank

と上記のエラー文が発生しました。

パラメーターで情報を送ることはできているが

but got errors:

以降の文を確認すると

Category can't be blank, Condition can't be blank, Postage payer can't be blank, Ship area can't be blank, Ship date can't be blank

とバリデーションでデータが弾き出されているとエラーが表示されている・・・

↓以下実装コード

ruby models/item.rb
class Item < ApplicationRecord
  validates :title,            presence: true
  validates :price,            presence: true, 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 :concept,          presence: true
  validates :image,            presence: true
  validates :category_id,      numericality: { other_than: 1 , message: "can't be blank" } 
  validates :condition_id,     numericality: { other_than: 1 , message: "can't be blank" }
  validates :postage_payer_id, numericality: { other_than: 1 , message: "can't be blank" }
  validates :ship_area_id,     numericality: { other_than: 1 , message: "can't be blank" }
  validates :ship_date_id,     numericality: { other_than: 1 , message: "can't be blank" }

  belongs_to :user
  has_one_attached :image

  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to :category
  belongs_to :condition
  belongs_to :postagepayer, optional: true
  belongs_to :shiparea,     optional: true
  belongs_to :shipdate,     optional: true
end
ruby factories/items.rb
FactoryBot.define do
  factory :item do
    title             { '商品名' }
    price             { '50000' }
    concept           { '説明文' }
    category_id       { '1' }
    condition_id      { '1' }
    postage_payer_id  { '1' }
    ship_area_id      { '1' }
    ship_date_id      { '1' }

    association :user

    after(:build) do |item|
      item.image.attach(io: File.open('public/images/test_image.png'), filename: 'test_image.png')
    end
  end
end

解説

ruby models/item.rb
  validates :category_id,      numericality: { other_than: 1 , message: "can't be blank" } 
  validates :condition_id,     numericality: { other_than: 1 , message: "can't be blank" }
  validates :postage_payer_id, numericality: { other_than: 1 , message: "can't be blank" }
  validates :ship_area_id,     numericality: { other_than: 1 , message: "can't be blank" }
  validates :ship_date_id,     numericality: { other_than: 1 , message: "can't be blank" }

で id1 意外で登録するように設定しているのに

FactoryBot作成時に
仮の数字を 1 で入力しているという初歩中の初歩のミスを犯してました・・・泣

ruby factories/items.rb
FactoryBot.define do
  factory :item do
    title             { '商品名' }
    price             { '50000' }
    concept           { '説明文' }
    category_id       { '2' }
    condition_id      { '2' }
    postage_payer_id  { '2' }
    ship_area_id      { '2' }
    ship_date_id      { '2' }

    association :user

    after(:build) do |item|
      item.image.attach(io: File.open('public/images/test_image.png'), filename: 'test_image.png')
    end
  end
end

以上 1→2に 修正することで無事解決しました。
解決に2時間ほどかかってしまいました。。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?