0

I'm writing rspec tests for my controller and trying to test the rendering of a partial, when the action is triggered by an AJAX requisition.

In my controller code, I have:

def new
  ...
  respond_to do |format|
    format.html { render action: "new" }
    format.js   { }
  end
end

I have the files new.html.erb and new.js.erb placed the correct view directory.

And my rspec test is the following:

it "should render the new partial" do
  get :new, :format => 'js'
  response.should render_template(:partial => 'new')
end

When executing this rspec test I get the following error:

Failure/Error: response.should render_template(:partial => 'new')
  expecting partial <"new"> but action rendered <[]>

Can somebody figure why I am getting this error? What am I doing wrong?

Thank you

4
  • it is correct! You send js request (get :new, :format => 'js') and this code format.js { } in controller works. and I think it should NOT render any partial. Commented May 28, 2014 at 6:10
  • But how can I test it does correctly render the new.js.erb contents? Commented May 28, 2014 at 12:34
  • maybe try to test response.body Commented May 28, 2014 at 12:37
  • try removing the block { }, so it becomes format.js only Commented Apr 28, 2015 at 7:51

1 Answer 1

4

According to rails guide, the syntax is slightly different:

http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#csrf-protection-from-remote-script-tags

Your test should be something like:

it 'assigns instance of Item to @item and renders new.js.erb' do  
    xhr :get, :new, format: :js
    expect(assigns[:item]).to be_an_instance_of Item
    expect(response).to render_template('new')
end

Your controller would look something like:

class ItemsController < ApplicationController
   def new
     @item = Item.new
   end
 end

Your view will be a .js.erb file

 items/new.js.erb
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.