Module: Rspec::Rails23::Mocking::WithRR

Defined in:
lib/rspec/rails23/mocking/with_rr.rb

Instance Method Summary collapse

Instance Method Details

#mock_model(model_class, options = {}) ⇒ Object



25
26
27
# File 'lib/rspec/rails23/mocking/with_rr.rb', line 25

def mock_model(model_class, options = {})
  mock_or_stub_model(model_class, 'mock', options)
end

#mock_or_stub_model(model_class, mock_or_stub, options = {}) {|model| ... } ⇒ Object

Creates a mock object instance for a model_class with common methods stubbed out. Additional methods may be easily stubbed (via add_stubs) if stubs is passed.

Yields:

  • (model)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rspec/rails23/mocking/with_rr.rb', line 9

def mock_or_stub_model(model_class, mock_or_stub, options = {})
  model = model_class.new
  model.extend ::Rspec::Rails23::Mocking::ModelStubber
  model_id = next_id

  stub(errors_stub = Object.new).count.returns(0)
  full_params = { :id => model_id, :new_record? => false, :errors => errors_stub }.update(options)
  
  full_params.each do |method, value|
    eval "#{mock_or_stub}(model).#{method}.returns(value)", binding, __FILE__, __LINE__
  end
  
  yield model if block_given?
  model
end

#stub_model(model_class, options = {}) ⇒ Object



29
30
31
# File 'lib/rspec/rails23/mocking/with_rr.rb', line 29

def stub_model(model_class, options = {})
  mock_or_stub_model(model_class, 'stub', options)
end