toshizou-Rails

RSpecで発生したエラーめも(随時追加予定)

この記事でやること

RSpecで発生したエラーとその解決方法を

開発環境

RequestSpecでのエラー

ArgumentError: x is not a valid column_name
# app/models/cuisine.rb
class Cuisine < ApplicationRecord
  enum difficulty: { easy: 0, normal: 1, hard: 2 }
  has_many :foodstuffs, dependent: :destroy
end

# spec/factories/cuisines.rb
FactoryBot.define do
  factory :cuisine do
    sequence(:name) {|n| "料理名#{n}" }
    # before(NG):
    #  この状態だと enum の value を返す
    sequence(:difficulty) {|n| n }
    # after(OK):
    #  ・keyを取得するように設定
    #  ・Cuisine.difficulties.keys で enumの値を全て取得して
    #   sampleメソッドでそのうちのどれかを返す
    difficulty { Cuisine.difficulties.keys.sample }
  end
end