Home>

I want to test the product information input function of furima, but the following error message is output in the terminal, and the unit test code of the model does not work. There seems to be a cause in the FactoryBot class, but when I looked it up, I couldn't find anything like this. I would like someone who understands.

    NoMethodError:
        undefined method `category_id ='for #<Item: 0x00007fe1f844ee18>
        Did you mean? Category =
                       category_was

It will be the following file.

spec/factories/items.rb
FactoryBot.define do
  factory: item do
    image {Faker :: Lorem.sentence}
    name {"name"}
    introduction {"description"}
    price {2000}
    item_condition_id {1}
    postage_id {1}
    prefecture_id {1}
    prepare_id {1}
    category_id {1}
    association: user
  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 does not work' do
      it'You can list if all values ​​are entered correctly' do
        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_messsages) .to include ("Image can't be blank")
      end
      it'Cannot list if name is empty' do
        @ item.name = nil
        @ 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_messsages) .to include ("Introduction can't be blank")
      end
      it'Cannot list if category is not selected' do
        @ item.category = 0
        @ item.valid?
        expect (@ item.errors.full_messsages) .to include ("Category can't be blank")
      end
      it'Cannot list if item_condition is not selected' do
        @ item.item_condition = 0
        @ item.valid?
        expect (@ item.errors.full_messsages) .to include ("Item_condition can't be blank")
      end
      it'Cannot list if postage is not selected' do
        @ item.postage = 0
        @ item.valid?
        expect (@ item.errors.full_messsages) .to include ("Postage can't be blank")
      end
      it'Cannot list if prefecture is not selected' do
        @ item.prefecture = 0
        @ item.valid?
        expect (@ item.errors.full_messsages) .to include ("Prefecture can't be blank")
      end
      it'Cannot list if prepare is not selected' do
        @ item.prepare = 0
        @ item.valid?
        expect (@ item.errors.full_messsages) .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_messsages) .to include ("Price can't be blank")
      end
      it'Cannot sell if price is full-width number' do
        @ item.price = "2000"
        @ item.valid?
        binding.pry
        expect (@ item.errors.full_messsages) .to include ("")
      end
    end
  end
end
  • Answer # 1

    Probably because there is no code when it succeeds,

        it'Cannot list if category is not selected' do
            @ item.category = 0
            @ item.valid?
            expect (@ item.errors.full_messsages) .to include ("Category can't be blank")
          end


    Is

        it'Cannot list if category is not selected' do
            @ item.category_id = 0
            @ item.valid?
            expect (@ item.errors.full_messsages) .to include ("Category can't be blank")
          end


    it might be?
    Please check.

Related articles