views:

68

answers:

1

I'm facing a problem with rspec and controllers

i'm spec'ing an update action, so to do it i call the following code:

put :update, :id => "1", :ntp => {:name=>'myservah'}

My controller has the following code:

def update
 if @ntp.update_attributes(params[:ntp])
  flash.now[:notice] = "Successfully updated ntp."
 else
  flash.now[:error] = ((errors_to_a(@ntp)*'.<br />')+'.')
 end
 render :partial => 'update'
end

And i'm getting the following error:

Failure/Error: put :update, :id => "1", :ntp => {:name=>'myservah'} Missing partial ntps/update with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "#"

My question is: should i explicit say which handler i have? Today my partial is named '_update.js.erb'.

BTW, my code works, i render the "js" version of update, since i just run some scripts

+3  A: 

Rails detects the format of the response from certain request parameters, including the http accept request header (intelligent guessing), the :format parameter or the page extension.

Unless specified, Rails default the request format to :html.

As you can see, your request defaults to :html which means Rails is trying to render the _update.html.erb template. There are a few changes you can apply to fix the issue:

  1. Always specify the request format in the test, if different than html.

    # emulate a js request
    put :update, :id => "1", :ntp => {:name=>'myservah'}, :format => :js
    

    If the action is intended to respond to an AJAX request, run the test using the xhr method

    # emulate an XHR request
    xhr :put, :update, :id => "1", :ntp => {:name=>'myservah'}, :format => :js
    
  2. Return 406 with any non-js response. Change render :partial => 'update' to

    respond_to do |format|
      format.js  { render :partial => 'update' }
      format.any { head 406 }
    end
    
Simone Carletti
i had to change just the controller and add the format.. thanks for the "406" tip.. btw the hxr syntax, at least in rspec 2.0.0 is wrong.
VP
Sorry, it's `xhr`. I fixed the answer.
Simone Carletti