![](/img/trans.png)
[英]rails validation: :allow_nil and :inclusion both needed at the same time
[英]Minitest for testing presence and allow_nil for the same attribute
我想測試一個模型屬性,該屬性具有真實的存在,但也允許nil值。 我該如何測試? 我在下面做了一個例子:
# Person model
belongs_to :city
validates :city_id, presence: true, :allow_nil => true
我正在嘗試測試:
test "should permit nil for city_id" do
@person.city_id = nil
assert @person.valid?
end
我在控制台中得到了什么:
期望假是真實的。
我找到了答案。 測試失敗,因為它丟失了belongs_to中的一個參數:
# Person model
belongs_to :city, optional: true
validates :city_id, presence: true, :allow_nil => true
參數Optional允許將nil值用作外鍵。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.