How to test rendering a partial with RSpec

27,007

Solution 1

Also try this

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

Solution 2

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

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

Solution 3

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

If using in rspec controllers

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

Solution 5

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

Share:
27,007
Pavel
Author by

Pavel

Ruby on rails developer

Updated on August 23, 2020

Comments

  • Pavel
    Pavel over 3 years

    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?

  • Fabio
    Fabio about 12 years
    It works, but you need to specify render_views if you're writing controller specs.
  • Abe Petrillo
    Abe Petrillo about 11 years
    Can you expand on the render_views thing? render_template isn't working for me in controller tests.
  • aNoble
    aNoble about 11 years
    render_views is simply called within the describe block.describe OrganizationsController do; render_views; ...
  • rouan
    rouan about 11 years
    With the latest version of rpec it may look more like this expect(response).to render_template(:partial => '_partial_name')
  • aceofbassgreg
    aceofbassgreg almost 11 years
    Here's some documentation regarding render_views: relishapp.com/rspec/rspec-rails/docs/controller-specs/…
  • c.apolzon
    c.apolzon almost 11 years
    Latest rspec will pass with expect(response).to render_partial("partial_name") rouan's approach also works.
  • Benj
    Benj over 10 years
    @c.apolzon I got undefined method 'render_partial' using today's updated rspec-rails ~> 2.14.0 and shoulda-matchers git-branch dp-rails-four. What gem are you using to get render_partial matcher? Thanks
  • jbielick
    jbielick about 9 years
    that would be a pretty fragile test -- such that the test would fail if you changed the html, but what you're really testing is whether or not the template gets rendered.
  • Argonus
    Argonus over 7 years
    Spec should test test rendering partial, not html body.
  • Kris Khaira
    Kris Khaira over 6 years
    Should you really be testing a view in a controller spec? RSpec provides spec/views for this; or you could use feature/integration tests.
  • Diego D
    Diego D over 6 years
    @KrisKhaira well, may you need both. In a controller spec you could test that the controller is going to render the expected template/view. Then in feature/integration tests you can deeply test the whole floe, in particular the view to test if user is able to perform expected actions.
  • Amit Patel
    Amit Patel over 6 years
    For rspec 3.6.0 use expect(response).to render_partial("_partial_name") but you have to use render_views within describe. Mind the _ in the name of the partial
  • lacostenycoder
    lacostenycoder about 4 years
    why do you need need CGI.unescape_html ?
  • coorasse
    coorasse about 4 years
    because if you check for a string that contains characters like ', it would fail otherwise