Resource Controller Spec failures for destroy actions?

4 views
Skip to first unread message

jnicklas

unread,
Apr 4, 2009, 10:12:51 AM4/4/09
to resource_controller
I am in the process of replacing existing controllers in an app with
resource controller. It works fine, but I am getting weird spec
failures I can’t explain. The failures happen for all destroy actions,
and only for destroy actions, and the error is the same in all
instances:

NoMethodError in ‘Admin::PriceCataloguesController DELETE destroy
redirects to the admin_price_catalogues list’ You have a nil object
when you didn’t expect it! The error occurred while evaluating
nil.call

Here’s a complete stacktrace along with the code and the spec that
makes it fail: http://gist.github.com/90212.

The code works fine, hitting the destroy action from the browser works
perfectly. I have dug through the code, but I can’t find the problem.
Does anyone have any insight, what might be going wrong?

Thanks,

/Jonas Nicklas

Chad Pytel

unread,
May 31, 2009, 12:24:25 PM5/31/09
to resource_controller
Hi,

I'm doing the same thing (replacing existing controllers in an app
with
resource controller) and I'm getting this exact same failure, but in
normal test:unit with shoulda.

Did you have any luck resolving this? Does anyone know what the
problem is?

thanks,
-Chad

James Golick

unread,
May 31, 2009, 12:26:29 PM5/31/09
to resource_...@googlegroups.com
Hey Chad,

Could you pastie the relevant controller code?

-J.

Chad Pytel

unread,
May 31, 2009, 12:44:26 PM5/31/09
to resource_...@googlegroups.com
Hey James,

Here is the relevant controller code, and the failing tests. I made
sure the same failure happens after removing the other controller code
I removed from this pastie.

http://pastie.org/495741

This app uses subdomains, so the current_account method you see there
is returning the account for the subdomain. I'm new to r_c as of
today, so its possible I'm not doing the object and collection
overrides write for this case.

thanks,
-Chad
---
Chad Pytel, Founder and CEO
thoughtbot, inc.
t: 617-482-1300 x113
f: 866-217-5992
http://www.thoughtbot.com

James Golick

unread,
May 31, 2009, 1:02:53 PM5/31/09
to resource_...@googlegroups.com
Hey Chad,

So, the reason you were seeing that failure is that there was no default response for a destroy failure. I just pushed a fix for that issue. But, it'll take a few to get to github's gem servers.

In the meantime, if you add your destroy fails response:

destroy.failure.wants.html { render :action => "edit" } # guessing from your pastie

That should get you on your way to making that test go green.

Hope that helps,

-J.

Jonas Nicklas

unread,
May 31, 2009, 3:46:12 PM5/31/09
to resource_...@googlegroups.com
Unfortunately it looks like this did not resolve my original issue. I'll investigate further if I find the time.

/Jonas Nicklas

James Golick

unread,
May 31, 2009, 5:02:59 PM5/31/09
to resource_...@googlegroups.com
Are you sure you updated correctly? I got the exact same backtrace when i wrote a failing test to try to solve this problem.

Jonas Nicklas

unread,
May 31, 2009, 5:27:44 PM5/31/09
to resource_...@googlegroups.com
Hmm, I misread the error messages, they have changed, now I'm getting:

'Admin::PriceCataloguesController DELETE destroy redirects to the admin_price_catalogues list' FAILED
expected redirect to "http://test.host/admin/price_catalogues", got redirect to "http://test.host/admin/price_catalogues/2"

Which has lead me to discover the root of my problem, and why this wasn't working in the first place. Resource_controller does this for destroy:

    def destroy
      load_object
      before :destroy
      if object.destroy
        after :destroy
        set_flash :destroy
        response_for :destroy
      else
        after :destroy_fails
        set_flash :destroy_fails
        response_for :destroy_fails
      end
    end

Since I was mocking out object.destroy and not returning anything, resource controller thought that it had failed. Since there was no failure response, the error appeared. So all I had to do was to return true in my mocks and all was well.

In short: all good, carry on!

Thanks for the fix!

/Jonas Nicklas
Reply all
Reply to author
Forward
0 new messages