Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec testing ajax response (should render a partial)

I want to test that my controller action is rendering a partial.

I've poked around and I can't seem to find anything that works.

create action:

def create
  @project = Project.new...
  respond_to do |format|
    if @project.save
      format.js { render :partial => "projects/form" }
    end
  end
end

spec:

it "should save and render partial" do
  ....
  #I expected/hoped this would work
  response.should render_partial("projects/form")
  #or even hopefully
  response.should render_template("projects/form")
  #no dice
end
like image 340
recursive_acronym Avatar asked Apr 15 '11 16:04

recursive_acronym


1 Answers

If you're looking for a REAL answer... (i.e. entirely in RSpec and not using Capybara), the RSpec documentation says that render_template is a wrapper on assert_template. assert_template (according to the docs) also indicates that you can check that a partial was rendered by including the :partial key.

Give this a go...

it { should render_template(:partial => '_partialname') }
like image 168
BlueFish Avatar answered Sep 22 '22 13:09

BlueFish