Skip to content

Instantly share code, notes, and snippets.

@mattheworiordan
Last active June 30, 2017 16:55
Show Gist options
  • Save mattheworiordan/9020970 to your computer and use it in GitHub Desktop.
Save mattheworiordan/9020970 to your computer and use it in GitHub Desktop.
Allow testing of locals when rendering Controllers
def assigns_local(key)
controller.instance_variable_get('@patched_locals')[key]
end
def patch_controller_render_to_support_locals(controller)
def controller.render(options = nil, extra_options = {}, &block)
[options, extra_options].select { |o| o.kind_of?(Hash) }.each do |option_hash|
if option_hash.include?(:locals)
@patched_locals = option_hash[:locals]
end
end
super(options, extra_options, &block)
end
end
class FooController < ApplicationController
def index
render locals: {
foos: Foo.all
}
end
end
require 'spec_helper'
describe FooController do
describe :index do
it 'should render locals' do
expect(response).to render_template('index')
expect(assigns_local(:foos)).to eq(Foo.all)
end
end
end
require 'support/assigns_locals_helper.rb'
config.before(:each, type: :controller) do
patch_controller_render_to_support_locals controller
end
@nruth
Copy link

nruth commented Jun 30, 2017

assert_template 'index', locals: { foos: Foo.all } seems to work too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment