0

私のモデルでは、次のテストを行います。

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

1 に答える 1

3

Model の外側で Model 定数を呼び出したいときはいつでも使用します<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) }
于 2012-09-17T04:10:24.830 に答える