ruby-on-rails - 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>
请帮我解决一下这个。
解决方案
推荐阅读
- bash - 并行运行两个连续的 bash 脚本 4 次
- facebook - 如何使用 API 通过 Facebook 获得赞助帖子?
- c# - 如何使用nearSphere mongodb驱动程序c#从到点返回距离结果
- android - 当 Retrofit 请求失败或成功时,如何在 Repository 和 ViewModel 类之间进行通信?
- adminlte - AdminLTE 3.0 如何知道 CardWidget 当前是打开还是关闭?
- django - 是否可以在 Django 中为特定 url 添加权限
- flexbox - Vuetify flex 包装到内容而不是行?
- javascript - 正则表达式以匹配自然语言中的十进制数
- python - ParserError:错误标记数据。C错误:内存不足
- python-3.x - 使用图像更改 image_1 的 roi(图像区域),而不会丢失 image_1 中的其他像素