Home>

The test code when I failed to enter the product information of furima works, but the test when it succeeds does not work.
Below is the error code.

   Failure/Error: expect (@item) .to be_valid
       expected #<Item id: nil, name: "name", introduction: "description", 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


It will be the following file.

spec/factories/items.rb
FactoryBot.define do
  factory: item do
    name {"name"}
    introduction {"description"}
    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'Product information input' do
    context'When product information input is successful' do
      it'You can sell if all the values ​​are entered correctly' do # This part
        expect (@item) .to be_valid
      end
    end
    context'When product information is not entered properly' do
      it'Cannot list if image is empty' do
        @ item.image = nil
        @ item.valid?
        expect (@ item.errors.full_messages) .to include ("Image can't be blank")
      end
      it'Cannot list if name is empty' do
        @ item.name = [email protected] item.valid?
        expect (@ item.errors.full_messages) .to include ("Name can't be blank")
      end
      it'Cannot list if introduction is empty' do
        @ item.introduction = nil
        @ item.valid?
        expect (@ item.errors.full_messages) .to include ("Introduction can't be blank")
      end
      it'Cannot list if category is not selected' do
        @ item.category_id = 0
        @ item.valid?
        expect (@ item.errors.full_messages) .to include ("Category can't be blank")
      end
      it'Cannot list if item_condition is not selected' do
        @ item.item_condition_id = 0
        @ item.valid?
        expect (@ item.errors.full_messages) .to include ("Item condition can't be blank")
      end
      it'Cannot list if postage is not selected' do
        @ item.postage_id = 0
        @ item.valid?
        expect (@ item.errors.full_messages) .to include ("Postage can't be blank")
      end
      it'Cannot list if prefecture is not selected' do
        @ item.prefecture_id = 0
        @ item.valid?
        expect (@ item.errors.full_messages) .to include ("Prefecture can't be blank")
      end
      it'Cannot list if prepare is not selected' do
        @ item.prepare_id = 0
        @ item.valid?
        expect (@ item.errors.full_messages) .to include ("Prepare can't be blank")
      end
      it'Cannot list if price is empty' do
        @ item.price = nil
        @ item.valid?
        expect (@ item.errors.full_messages) .to include ("Price can't be blank")
      end
      it'Cannot sell if price is full-width number' [email protected] item.price = "2000"
        @ item.valid?
        expect (@ item.errors.full_messages) .to include ("Price Half-width number.")
      end
      it'Cannot sell if price is outside the set range' 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

There was no problem with the table or migration file.
I look forward to working with you as a professor.

  • Answer # 1

    Isn't it in that message?
    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 is required
    to factory
    postage_id {1} prefecture_id {1} prepare_id {1} category_id
    It looks like it is in the form of
    The validation of postage, prefecture, prepare, category is written, but this is a snake. Not needed because there is belongs_to.
    The image condition is not in the bot.
    If the success is not successful, you will not know whether the failure is correct or not, so be careful.