【问题标题】:Name error for ensure inclusion of?确保包含的名称错误?
【发布时间】:2012-09-17 04:08:06
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    当您想在模型之外调用模型常量时,请使用 &lt;ModelName&gt;::&lt;ConstantVariableName&gt;

    改变

    UNIT_TYPES
    

    User::UNIT_TYPES #Assuming 'User' is your Model Name
    

    所以你的shoulda 代码应该如下所示

    it { should ensure_inclusion_of(:unit_type).in_array(User::UNIT_TYPES) }
    

    【讨论】:

      猜你喜欢
      • 2014-11-10
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      • 2013-08-31
      • 1970-01-01
      相关资源
      最近更新 更多