gpt4 book ai didi

ruby-on-rails - 如何对任意列表使用 ensure_inclusion_of

转载 作者:数据小太阳 更新时间:2023-10-29 08:25:01 24 4
gpt4 key购买 nike

在我的 Rails 项目中,我有一个属性为 foo 的模型。我有一个确保 foo 的任何值都在数组中的验证。这使用带有 :in 选项的验证。

我想用 rspec/shoulda 测试一下。我认为 ensure_inclusion_of 应该测试这个验证,但它只适用于范围而不是数组。我怎样才能让它与数组一起工作或编写我自己的匹配器来做到这一点?

最佳答案

ensure_inclusion_of 用于检查模块是否包含在当前类中。

如果您想测试 validates_inclusion_of,您可以自己编写 RSpecs,或者安装 Shoulda并使用它的宏。

我在这里验证用户模型上的 gender 字段。

describe User do

it { should validate_presence_of(:gender) }
it { should allow_value("male").for(:gender) }
it { should allow_value("female").for(:gender) }
it { should_not allow_value("other").for(:gender) }

end

关于ruby-on-rails - 如何对任意列表使用 ensure_inclusion_of,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5534438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com