Module: Spec::Rails::Example::RenderObserver

Included in:
ActionView::Base, ControllerExampleGroup::ControllerInstanceMethods, ViewExampleGroupController
Defined in:
lib/spec/rails/example/render_observer.rb

Overview

Extends the #should_receive, #should_not_receive and #stub! methods in rspec’s mocking framework to handle #render calls to controller in controller examples and template and view examples

Instance Method Summary collapse

Instance Method Details

#register_verify_after_eachObject

:nodoc:



70
71
72
73
# File 'lib/spec/rails/example/render_observer.rb', line 70

def register_verify_after_each #:nodoc:
  proc = verify_rendered_proc
  Spec::Example::ExampleGroup.after(:each, &proc)
end

#render_proxyObject

:nodoc:



75
76
77
# File 'lib/spec/rails/example/render_observer.rb', line 75

def render_proxy #:nodoc:
  @render_proxy ||= Spec::Mocks::Mock.new("render_proxy")
end

#should_not_receive(*args) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/spec/rails/example/render_observer.rb', line 31

def should_not_receive(*args)
  if args[0] == :render
    register_verify_after_each
    render_proxy.should_not_receive(:render)
  else
    super
  end
end

#should_receive(*args) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/spec/rails/example/render_observer.rb', line 22

def should_receive(*args)
  if args[0] == :render
    register_verify_after_each
    render_proxy.should_receive(:render, :expected_from => caller(1)[0])
  else
    super
  end
end

#stub(*args) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/spec/rails/example/render_observer.rb', line 40

def stub(*args)
  if args[0] == :render
    register_verify_after_each
    render_proxy.stub(args.first, :expected_from => caller(1)[0])
  else
    super
  end
end

#stub!(*args) ⇒ Object

FIXME - for some reason, neither alias nor alias_method are working as expected in the else branch, so this is a duplicate of stub() above. Could delegate, but then we’d run into craziness handling :expected_from. This will have to do for the moment.



53
54
55
56
57
58
59
60
# File 'lib/spec/rails/example/render_observer.rb', line 53

def stub!(*args)
  if args[0] == :render
    register_verify_after_each
    render_proxy.stub!(args.first, :expected_from => caller(1)[0])
  else
    super
  end
end

#unregister_verify_after_eachObject

:nodoc:



15
16
17
18
19
20
# File 'lib/spec/rails/example/render_observer.rb', line 15

def unregister_verify_after_each #:nodoc:
  proc = verify_rendered_proc
  #fix exception with undefined method `remove_after' for Spec::Example::ExampleGroup:Class
  Spec::Example::ExampleGroup.after_each_parts.delete(proc)
  #Spec::Example::ExampleGroup.remove_after(:each, &proc)
end

#verify_renderedObject

:nodoc:



11
12
13
# File 'lib/spec/rails/example/render_observer.rb', line 11

def verify_rendered # :nodoc:
  render_proxy.rspec_verify
end

#verify_rendered_procObject

:nodoc:



62
63
64
65
66
67
68
# File 'lib/spec/rails/example/render_observer.rb', line 62

def verify_rendered_proc #:nodoc:
  template = self
  @verify_rendered_proc ||= Proc.new do
    template.verify_rendered
    template.unregister_verify_after_each
  end
end