开发者

How do I write a functional test for the update action in a controller?

With actions such as :show and :edit, I can just http GET them. But since the :update action has no template, I can't issue a GET without it complaining about the missing template. That makes sense, so I tried to directly call @controller.update in my test, but I get an error on the respond_to block:

NoMethodError: undefined method `parameters' for nil:NilClass

How do I specify the requested format to be :html? I am passing in a parameter hash for the model's attributes, FWIW. Thanks for any advice.

Edit: I since tried invoking @controller.update (after setting params[]) in the console and that error quoted above bubbled up from here: /actionpack-2.3.8/lib/action_controller/mime_responds.rb

I also tried PUT instead of GET, but still got "Missing Template update.erb".

Also, I managed to find out that this error of missing template is caused by having anything other than a redirect or render in the code block marked HERE:

respond_to do |format|
  if @model.update_attributes(params[:model])
     flash[:notice] = 'note!'
     format.html { **HERE** }
     ...

I have a method call there that is just a conditional to redirect to the proper place. 开发者_StackOverflowI can, in principle, understand why it might look for a template to render since it may not be aware that there is a redirect further downstream, but what I don't understand is how come my web application doesn't run into the same error as my functional test.


  • You should be calling put :update in your test. If you have code like @my_model.update_attributes(params[:my_model]) in the controller then you would call put :update, :id => <whatever>, :my_model => {:foo => 'bar'} in your test.
  • :html is the default format, so you don't need to specify it. If you really wanted to, you could do put :update, :format => :html. But again, not necessary.
  • As Emily said, it's params, not parameters, so you might be accessing the wrong hash

Overall this question could use some more detail about what you're trying to do and what your current code looks like.


I think your basic problem is that the update method isn't usually a get request at all. You should be posting to that action.

The error you're getting sounds like you're running into more serious problems, though. Rails uses params, not parameters, so that's a pretty odd error. I don't know if this is related to calling the method directly on the controller and therefore not having some other context Rails is expecting or whether it's an additional problem. If switching to post doesn't fix it, please update your question with the body of your test.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜