Class: CouchPotato::RSpec::StubView::ViewStub

Inherits:
Object
  • Object
show all
Includes:
RSpec::Mocks::ExampleMethods
Defined in:
lib/couch_potato/rspec/stub_db.rb

Instance Method Summary collapse

Constructor Details

#initialize(clazz, view, db) ⇒ ViewStub

Returns a new instance of ViewStub.



8
9
10
11
12
# File 'lib/couch_potato/rspec/stub_db.rb', line 8

def initialize(clazz, view, db)
  @clazz = clazz
  @view = view
  @db = db
end

Instance Method Details

#and_return(return_value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/couch_potato/rspec/stub_db.rb', line 20

def and_return(return_value)
  view_stub = double("#{@clazz}.#{@view}(#{@args.try(:join, ', ')}) view")
  stub = allow(@clazz).to receive(@view)
  stub.with(*@args) if @args
  stub.and_return(view_stub)
  allow(@db).to receive(:view).with(view_stub).and_return(return_value)
  return unless return_value.respond_to?(:first)
  allow(@db).to receive(:first).with(view_stub).and_return(return_value.first)
  if return_value.first
    allow(@db).to receive(:first!).with(view_stub).and_return(return_value.first)
  else
    allow(@db).to receive(:first!).with(view_stub).and_raise(CouchPotato::NotFound)
  end
end

#with(*args, &block) ⇒ Object



14
15
16
17
18
# File 'lib/couch_potato/rspec/stub_db.rb', line 14

def with(*args, &block)
  @args = args
  and_return(block.call) if block
  self
end