How to test rendering a partial with RSpec

Also try this

response.should render_template(:partial => 'partial_name')

Latest rspec version suggest to use expect syntax rather than should:

expect(response).to render_template(partial: 'partial_name')

If you are testing this inside a controller you should do something like this:

RSpec.describe Users::RegistrationsController, type: :controller do
  describe "GET #new" do
    render_views

    it "render customer partial" do
      get :new
      expect(response).to render_template :new
      expect(response).to render_template(partial: '_new_customer')
    end
  end
end

Note that we need render_views as reported into documentation.

And this is the line that will test if "_new_customer" partial is rendered:

expect(response).to render_template(partial: '_new_customer')

You need to provide the name of the partial with the initial underscore.

Also be careful because in your code the IF and the ELSE statements are rendering the same thing.


If using in rspec controllers

expect(response).to render_template(partial: 'home/_sector_performance')