51

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?

3

7 Answers 7

71

Also try this

response.should render_template(:partial => 'partial_name')
8
  • 9
    It works, but you need to specify render_views if you're writing controller specs.
    – Fabio
    Apr 23, 2012 at 16:08
  • Can you expand on the render_views thing? render_template isn't working for me in controller tests. Feb 12, 2013 at 16:15
  • 3
    render_views is simply called within the describe block.describe OrganizationsController do; render_views; ...
    – aNoble
    Apr 7, 2013 at 19:11
  • 23
    With the latest version of rpec it may look more like this expect(response).to render_template(:partial => '_partial_name')
    – rouan
    Apr 12, 2013 at 5:50
  • 3
    Here's some documentation regarding render_views: relishapp.com/rspec/rspec-rails/docs/controller-specs/… May 24, 2013 at 17:36
34

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

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

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.

2
  • Should you really be testing a view in a controller spec? RSpec provides spec/views for this; or you could use feature/integration tests. Nov 16, 2017 at 12:22
  • @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.
    – Diego D
    Nov 17, 2017 at 13:28
1

If using in rspec controllers

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

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

0

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')
2
  • why do you need need CGI.unescape_html ? Apr 8, 2020 at 16:53
  • because if you check for a string that contains characters like ', it would fail otherwise
    – coorasse
    Apr 9, 2020 at 10:09
-4

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")
2
  • 2
    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.
    – jbielick
    Apr 2, 2015 at 16:47
  • Spec should test test rendering partial, not html body.
    – Argonus
    Dec 6, 2016 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.