How to test rendering a partial with RSpec

Ruby on-RailsRubyRuby on-Rails-3Rspec

Ruby on-Rails Problem Overview


I want to test rendering a particular partial according to some conditions.

For example, in model show action view show.html.erb I have:

<% if condition1 %>
 <%=  render :partial => "partial1" %>
<% else %>
 <%=  render :partial => "partial1" %>
<% end %>

I tried:

response.should render_template("partial_name")

but it tells that it rendered "show" template

> expecting <"partial1"> but rendering with <"model/show, > layouts/application">

What I am doing wrong?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Also try this

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

Solution 2 - Ruby on-Rails

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

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

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

If using in rspec controllers

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

Solution 5 - Ruby on-Rails

You can also test, if your controller inferred required action.

require "spec_helper"

describe "model_name/new.html.erb" do
 it "infers the controller path" do
  expect(controller.request.path_parameters["action"]).to eq("new")
 end
end

The docs are here

Solution 6 - Ruby on-Rails

Starting from Rails 5.1, this kind of test is discouraged and you should test the controller and the view as a whole.

Checking which partial is rendered by the controlled is part of the implementation details you shouldn't test.

Therefore I suggest you to write a request test and check that some relevant text of your partial is present in the response body.

get root_path
expect(CGI.unescape_html(response.body)).to include('Hello World')

Solution 7 - Ruby on-Rails

Instead of the above mentioned solution you could check alternatively, if the html that the partial renders is present. E.g.

response.body.should have_tag("div#foo")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPavelView Question on Stackoverflow
Solution 1 - Ruby on-RailsRishav RastogiView Answer on Stackoverflow
Solution 2 - Ruby on-RailsSeb WilgoszView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDiego DView Answer on Stackoverflow
Solution 4 - Ruby on-Railsuser2238766View Answer on Stackoverflow
Solution 5 - Ruby on-RailsDendeView Answer on Stackoverflow
Solution 6 - Ruby on-RailscoorasseView Answer on Stackoverflow
Solution 7 - Ruby on-RailsauralbeeView Answer on Stackoverflow