In Rails 3, I'm getting a NoMethodError for the recycle! method during testing
Asked Answered
P

3

23

I've got a functional test that's using fixtures. I also am using fixtures in my Unit tests, but they work without flaw. When running the functional tests, I get a:

NoMethodError: undefined method 'recycle!' for #<Response:0x10346be10> /test/functional/responses_controller_test.rb:10:in 'test_testing'

My functional tests, at this point, are doing nothing more than a get to the index action. Example:

setup do
  @response = responses(:one)
end

test "testing" do
  get :index
  assert true
end

My TestHelper class does include all fixtures, so the Responses fixtures are definitely loading. And like I said, the fixtures work perfectly fine in Unit tests.

Any idea what might be causing this?

Pitiable answered 2/10, 2010 at 15:51 Comment(0)
G
45

Change

setup do
  @response = responses(:one)
end

to

setup do
  @myresponse = responses(:one)
end

and off you go!

The problem lies in "actionpack-3.0.1/lib/action_controller/test_case.rb" around line 386.

The testcase assumes that @response holds a "ActionDispatch::Response". You overwrite it, it's no "ActionDispatch::Response" anymore and the testcase fails.

I'm not sure if this is intended behaviour.

Anyway, just make sure you don't overwrite @response/@request/@controller/@routes and it should work.

Gault answered 15/11, 2010 at 17:6 Comment(2)
Thanks, just saved my bacon. Had a similar issue with a variable called @request.Stearic
Wow! Had exactly the same code as the OP, and now I have exactly the same code as your solution! @GaultBeefsteak
C
2

Flo's answer is mostly correct, but I just wanted to clarify:

In Rails 3.0.x that @request is expected to be an ActionController::TestRequest type, which has the recycle! method defined on it. There's TestResponse, TestRequest, and TestSession.

You might want to actually initialize or manipulate objects of these types for certain situations. In my case, I needed to mock the site having a specific domain name with

@request = ActionController::TestRequest.new unless @request
@request.host = "www.hostname.com"
Canikin answered 25/8, 2011 at 22:48 Comment(0)
I
2

I just had a similar problem naming a variable @request. I changed to @_request and it solved the problem.

Inactive answered 13/5, 2014 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.