在我的模型中,我有以下测试:
UNIT_TYPES = [ 'seconds', 'minutes', 'hours', ]
validates_inclusion_of :unit_type, :in => UNIT_TYPES, :allow_blank => true
并使用shoulda-matchers我把:
it { should ensure_inclusion_of(:unit_type).in_array(UNIT_TYPES) }
但为什么我会收到此错误?
故障:
1) Price inclusions
Failure/Error: it { should ensure_inclusion_of(:unit_type).in_array(UNIT_TYPES) }
NameError:
uninitialized constant UNIT_TYPES
# ./spec/models/price_spec.rb:39:in `block (3 levels) in <top (required)>'
答案 0 :(得分:3)
每当您想要将模型常量调用到外,模型使用<ModelName>::<ConstantVariableName>
更改
UNIT_TYPES
要
User::UNIT_TYPES #Assuming 'User' is your Model Name
因此,您的shoulda
代码应该类似于
it { should ensure_inclusion_of(:unit_type).in_array(User::UNIT_TYPES) }