确保包含的名称错误?

时间:2012-09-17 04:08:07

标签: ruby-on-rails ruby ruby-on-rails-3 rspec rspec-rails

在我的模型中,我有以下测试:

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)>' 

1 个答案:

答案 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) }