How to test rendering a partial with RSpec
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
Comments
-
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 about 12 yearsIt works, but you need to specify
render_views
if you're writing controller specs. -
Abe Petrillo about 11 yearsCan you expand on the render_views thing? render_template isn't working for me in controller tests.
-
aNoble about 11 years
render_views
is simply called within the describe block.describe OrganizationsController do; render_views; ...
-
rouan about 11 yearsWith the latest version of rpec it may look more like this
expect(response).to render_template(:partial => '_partial_name')
-
aceofbassgreg almost 11 yearsHere's some documentation regarding
render_views
: relishapp.com/rspec/rspec-rails/docs/controller-specs/… -
c.apolzon almost 11 yearsLatest rspec will pass with
expect(response).to render_partial("partial_name")
rouan's approach also works. -
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 about 9 yearsthat 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 over 7 yearsSpec should test test rendering partial, not html body.
-
Kris Khaira over 6 yearsShould 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 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 over 6 yearsFor rspec
3.6.0
useexpect(response).to render_partial("_partial_name")
but you have to userender_views
withindescribe
. Mind the_
in the name of the partial -
lacostenycoder about 4 yearswhy do you need need
CGI.unescape_html
? -
coorasse about 4 yearsbecause if you check for a string that contains characters like
'
, it would fail otherwise