Rspec - check what rails view makes concrete partial

I have a rails 3.2.13 application that works with rspec-rails 2.14.0, and I'm trying to confirm that the view displays a specific partial in my test. It really works, but I need to add this test. Here is what I still have:

require 'spec_helper'

describe 'users/items/index.html.haml' do
  let(:current_user) { mock_model(User) }

  context 'when there are no items for this user' do
    items = nil

    it 'should render empty inventory partial' do
      response.should render_template(:partial => "_empty_inventory")
    end

  end
end

This runs without errors, but does not work. Mistake:

Failure/Error: response.should render_template(:partial => "_empty_inventory")
   expecting partial <"_empty_inventory"> but action rendered <[]>

Thanks for any ideas.

EDIT

This works for me, but Peter's solution is better ...

context 'when there are no items for this user' do

  before do
    view.stub(current_user: current_user)
    items = nil
    render
  end

  it 'should render empty inventory partial' do
    view.should render_template(:partial => "_empty_inventory")
  end

end 

For some reason, I wasn’t interested in calling renderfor viewing, but there you go ...

+4
source share
1 answer

, , , . , , _empty_inventory parial " ". , :

  it "displays the empty inventory message" do
    render
    rendered.should_not have_content('There is no inventory')
  end

, render_views . -

it 'should render empty inventory partial' do
  get :index, :user_id => user.id
  response.should render_template(:partial => "_empty_inventory")
end

, contoller.

+7

All Articles