Module | Spec::Rails::Example::RenderObserver |
In: |
vendor/plugins/rspec-rails/lib/spec/rails/example/render_observer.rb
|
Provides specialized mock-like behaviour for controller and view examples, allowing you to mock or stub calls to render with specific arguments while ignoring all other calls.
Similar to mocking render with the exception that calls to render with any other options are passed on to the receiver (i.e. controller in controller examples, template in view examples).
This is necessary because Rails uses the render method on both controllers and templates as a dispatcher to render different kinds of things, sometimes resulting in many calls to the render method within one request. This approach makes it impossible to use a normal mock object, which is designed to observe all incoming messages with a given name.
expect_render is auto-verifying, so failures will be reported without requiring you to explicitly request verification.
Also, expect_render uses parts of RSpec‘s mock expectation framework. Because it wraps only a subset of the framework, using this will create no conflict with other mock frameworks if you choose to use them. Additionally, the object returned by expect_render is an RSpec mock object, which means that you can call any of the chained methods available in RSpec‘s mocks.
controller.expect_render(:partial => 'thing', :object => thing) controller.expect_render(:partial => 'thing', :collection => things).once controller.stub_render(:partial => 'thing', :object => thing) controller.stub_render(:partial => 'thing', :collection => things).twice
template.expect_render(:partial => 'thing', :object => thing) template.expect_render(:partial => 'thing', :collection => things) template.stub_render(:partial => 'thing', :object => thing) template.stub_render(:partial => 'thing', :collection => things)
# File vendor/plugins/rspec-rails/lib/spec/rails/example/render_observer.rb, line 46 46: def expect_render(opts={}) 47: register_verify_after_each 48: expect_render_mock_proxy.should_receive(:render, :expected_from => caller(1)[0]).with(opts) 49: end
This is exactly like expect_render, with the exception that the call to render will not be verified. Use this if you are trying to isolate your example from a complicated render operation but don‘t care whether it is called or not.
# File vendor/plugins/rspec-rails/lib/spec/rails/example/render_observer.rb, line 54 54: def stub_render(opts={}) 55: register_verify_after_each 56: expect_render_mock_proxy.stub!(:render, :expected_from => caller(1)[0]).with(opts) 57: end