Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails测试跳过自定义验证还是自定义验证不起作用?_Ruby On Rails_Validation_Testing_Attributes - Fatal编程技术网

Ruby on rails Rails测试跳过自定义验证还是自定义验证不起作用?

Ruby on rails Rails测试跳过自定义验证还是自定义验证不起作用?,ruby-on-rails,validation,testing,attributes,Ruby On Rails,Validation,Testing,Attributes,我有一个带有布尔属性:fee的模型订阅,在创建订阅后不应修改该属性 我添加了如下自定义验证: validate :fee_not_changed def fee_not_changed return unless created_at != updated_at && fee_changed? errors.add :fee, :changed, message: "cannot be changed" end 我们的API不允许我们更新:fee

我有一个带有布尔属性
:fee
的模型
订阅
,在创建
订阅
后不应修改该属性

我添加了如下自定义验证:

validate :fee_not_changed

def fee_not_changed
  return unless created_at != updated_at && fee_changed?

  errors.add :fee, :changed, message: "cannot be changed"
end
我们的API不允许我们更新
:fee
(返回错误),但我可以在测试中更新属性,尽管有如下自定义验证:

test "fee cant be edited after creation" do
  subscription = subscriptions(:with_fee)
  subscription.update!(fee: false)

  assert_not subscription.saved_change_to_fee?
end

Expected true to be nil or false
另外,如果我在断言之前放置一个
byebug
,我可以看到它有效地更新了跳过自定义验证的属性


有什么问题吗?感谢您提供的帮助。

我认为这是一个缓存问题

是否可以尝试使用
重新加载订阅。重新加载


assert\u not subscription.reload.saved\u change\u to\u fee?

您也可以共享设备吗?谢谢工作,谢谢!