怎么可能? 在订单模型:
validates :pay_type_id, inclusion: PayType.ids
在测试中:
patch :update, id: @order, order: { address: @order.address, email: @order.email, name: @order.name, pay_type_id: PayType.ids.first }
assert_equal [], PayType.ids
测试结果:
1) Error:
OrdersControllerTest#test_should_create_order:
ActiveRecord::RecordInvalid: Validation failed: Pay type is not included in the list
app/controllers/orders_controller.rb:41:in `block in create'
app/controllers/orders_controller.rb:40:in `create'
test/controllers/orders_controller_test.rb:36:in `block (2 levels) in <class:OrdersControllerTest>'
test/controllers/orders_controller_test.rb:35:in `block in <class:OrdersControllerTest>'
2) Failure:
OrdersControllerTest#test_should_deny_update_if_not_logged_in [/home/eugenes/Dropbox/work/depot/test/controllers/orders_controller_test.rb:85]:
Expected: []
Actual: [298486374, 980190962]
在开发环境中一切都好。 我做错了什么?
答案 0 :(得分:0)
您可以执行类似
的操作 validates_inclusion_of :pay_type_id, :in => lambda { |_| PayType.pluck(:id) }
至少这样,每次验证器运行时,您都会对数据库进行新的查询并获取最新的PayType ID。
PS。由于存在错误而包含虚拟参数_
- 对于后续操作,请查看https://github.com/rails/rails/issues/13689。