How to check locales of a rendering template in rspec

I wonder how to check the locales passed to the render pattern in the controller

Controller:

def lelf_panel
  # ...
  if some_condition
    locals_hash = some_very_long_hash_A
  else
    locals_hash = some_very_long_hash_B
  end
  render :partial => "left_panel", :layout => false, :locals => locals_hash
end

Current specification:

it 'should render correct template for lelf_panel' do
  # ... 
  get 'left_panel'
  response.should render_template('system/_left_panel')
end   

Now I need to finish Rcov for this controller, so I need to add / change the specification to cover both of some_condition results. and I want to check the "lelf_panel" locales passed to render, as if I only checked render_template, the partial page displayed for both results is the same.

I check 'render_template' in rspec docs at http://rubydoc.info/gems/rspec-rails/2.8.1/RSpec/Rails/Matchers/RenderTemplate:render_template

it provides only the 2nd parameter for the message, so how can I check the locales passed to the rendering?

+5
3

, , .

locals_hash @locals_hash, assigns (: locals_hash).

HTML , - - , locals_hash , , HTML- , .

+1

render_template .

it 'should render correct template for lefl_panel' do
  # ...
  allow(controller).to receive(:render).with no_args
  expect(controller).to receive(:render).with({
    :partial => 'system/_left_panel',
    :layout  => false,
    :locals  => some_very_long_hash_A
  })
  get 'left_panel'
end
+14

, @ryan-ahearn @user2490003, RSpec 3.

  # Safe to set globally, since actions can either render or redirect once or fail anyway
  before do
    allow(controller).to receive(:render).and_call_original
  end

  describe "get left panel" do
    before do
      # other setup
      get 'left_panel'
    end

    it 'should render correct template for lelf_panel' do
      # Sadly, render_template is primitive (no hash_including, no block with args, etc.)
      expect(subject).to render_template('system/_left_panel')
    end

    it 'should render with correct local value' do
      expect(controller).to have_received(:render) do |options|
        expect(options[:locals][:key_from_very_long_hash]).to eq('value_of_key_from_very_long_hash')
      end
    end
  end
+1

All Articles