Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test rendering a partial with RSpec

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?

like image 695
Pavel Avatar asked Mar 30 '12 16:03

Pavel


People also ask

How do I run a RSpec test on a file?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.

Is RSpec TDD or BDD?

RSpec is a Behavior-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.

What is render partial in Ruby?

Ruby on Rails Views Partials Partial templates (partials) are a way of breaking the rendering process into more manageable chunks. Partials allow you to extract pieces of code from your templates to separate files and also reuse them throughout your templates.

What type of testing is RSpec?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.


2 Answers

Also try this

 response.should render_template(:partial => 'partial_name') 
like image 124
Rishav Rastogi Avatar answered Sep 20 '22 12:09

Rishav Rastogi


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

expect(response).to render_template(partial: 'partial_name') 
like image 21
swilgosz Avatar answered Sep 20 '22 12:09

swilgosz