Module: Spec::Rails::Mocks

Defined in:
lib/polymorphic_model/spec/rails/mocks.rb

Defined Under Namespace

Classes: InvalidPolymorphicModelTypeError

Instance Method Summary collapse

Instance Method Details

#mock_polymorphic_model(model_class, model_type, options_and_stubs = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/polymorphic_model/spec/rails/mocks.rb', line 10

def mock_polymorphic_model(model_class, model_type, options_and_stubs={})
  raise InvalidPolymorphicModelTypeError unless model_class.types.include?(model_type)
  if block_given?
    m = mock_model(model_class, options_and_stubs, &block)
  else
    m = mock_model(model_class, options_and_stubs)
  end

  model_class.types.each do |t|
    m.stub!(:"#{t}?").and_return(t == model_type ? true : false)
  end  
  polymorphic_column = model_class.instance_eval { @_polymorphic_column }
  m.stub!(polymorphic_column).and_return(model_type.to_s)
  return m 
end