0

Trying to figure out how to test my partial that calls another partial twice with different parameters. I'd like my test to verify those parameters in the two different calls.

The two partial calls in the main partial under test:

<div class="col-md-10">
<legend>Dates</legend>
<div class="form-group">
  <%= f.label :start_date %>
  <%= render partial: 'shared/common_date_time_chooser', locals: {prefix: :start_date, initial_time: Time.zone.now.beginning_of_day} %>
</div>
<div class="form-group">
  <%= f.label :end_date %>
  <%= render partial: 'shared/common_date_time_chooser', locals: {prefix: :end_date, initial_time: Time.zone.now.end_of_day} %>
</div>

So I want to verify that the two calls are made with the two different prefix and initial_time parameters.

The testing code renders like such:

require 'spec_helper'
describe 'notices/_form' do
  subject do
    assign(:notice, notice)
    render
  end

If the main partial made a single call to the 'common_date_time_chooser' partial I could use should render_template which has a version in view specs that let's you test for passed parameters. However, that defers to Rails' assert_template which seems to only keep track of the latest call.

In controller tests I have done something like

controller.should_receive(:render).
        with({partial: 'shared/redirect_from_ajax', locals: {path: target_path, modal_id: modal_id}}).
        and_call_original

but so far in the view test I can't figure out on what to put the should_receive and what the expected received method is.

Seems like this 'twice called partial in a view partial' scenario doesn't have a way to test AFAIK.

All help/suggestions appreciated.

user2391694
  • 161
  • 1
  • 5

1 Answers1

0

From the documentation:

Given a file named spec/views/gadgets/index.html.erb_spec.rb with:

require "spec_helper"

describe "gadgets/index" do
  it "renders the index template" do
    assign(:gadgets, [stub_model(Gadget)])
    render

    expect(view).to render_template(:index)
    expect(view).to render_template("index")
    expect(view).to render_template("gadgets/index")
  end
end

When I run rspec spec/views
Then the examples should all pass

In your case that means:

expect(view).to render_template(partial: 'shared/common_date_time_chooser', locals: {prefix: :start_date, initial_time: Time.zone.now.beginning_of_day})
expect(view).to render_template(partial: 'shared/common_date_time_chooser', locals: {prefix: :end_date, initial_time: Time.zone.now.end_of_day})
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
  • What the documentation is showing is three different ways to access a single render (it's also a render of a view and not a partial). That's not my situation. I'm trying to match 2 different renders. I tried exactly what you suggested (but as 2 separate examples) and it recognized the last partial call to be made but not the first. That appears to be a limitation of Rails' assert_template as far as I can tell. – user2391694 Apr 07 '14 at 21:26
  • Clarifying (couldn't seem to edit the comment itself now): I'm trying to match 2 different renders of the same partial. – user2391694 Apr 07 '14 at 21:41
  • What happens when you put the two expectations in one test? – Uri Agassi Apr 08 '14 at 04:41
  • Whichever expectation that is listed second gets recognized, apparently overwriting the first expectation. Switching the expectation order switches what is recognized. – user2391694 Apr 08 '14 at 20:38