首页 > 解决方案 > Rails/Rspec:未定义的方法 `temp_val' 用于#

问题描述

我在 rspec 中收到此错误。这些是必需的文件

# app/models/general_class.rb

class GeneralClass < ApplicationRecord
  validates_with CustomValidator
  validates_with CustomValidator, if: Proc.new { |obj| obj.temp_val.present? }
  validates_presence_of :temp_val_expiry, if: Proc.new { |obj| obj.temp_val.present? }
# in db/schema.rb

create_table "general_class" do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "field1"
    t.text "field2"
    t.text "temp_val"
    t.datetime "temp_val_expiry"
  end
# spec/models/general_class.rb

  describe '#test' do
    it 'description' do
      gs = GeneralClass.create(field: "dummy", field2: "value")
      gs.temp_val = 'new_val_temp'
      gs.temp_val_expiry = 1.day.after
      gs.save!
    end
  end

当我运行上述规范时,我得到了这个

     Failure/Error: validates_with CustomValidator, if: Proc.new { |obj| obj.temp_val.present? }

     NoMethodError:
       undefined method `temp_val' for #< GeneralClass:0x00007fb8eb95ce40>

请帮我解决一下这个。

标签: ruby-on-railsrspecruby-on-rails-6

解决方案


推荐阅读