2010-10-02 19 views
22

我有一个使用灯具的功能测试。我也在我的单元测试中使用灯具,但他们的工作没有缺陷。当运行功能测试,我得到一个:在Rails 3中,我收到了一个N​​oMethodError的回收!方法测试

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

功能测试,在这一点上,所做的仅仅是多了去索引操作。例如:

setup do 
    @response = responses(:one) 
end 

test "testing" do 
    get :index 
    assert true 
end 

我的TestHelper类确实包括所有的灯具,所以响应灯具肯定是加载。就像我说的,在单元测试中,灯具工作得很好。

任何想法可能会造成这种情况?

回答

44

变化

setup do 
    @response = responses(:one) 
end 

setup do 
    @myresponse = responses(:one) 
end 

和您去!

问题就出在 “ActionPack的-3.0.1/lib目录/ action_controller/test_case.rb” 绕线386

测试用例假设@应答持有 “ActionDispatch ::响应”。您覆盖它,它不再是“ActionDispatch :: Response”,并且测试用例失败。

我不确定这是否是有意的行为。

无论如何,只要确保您不覆盖@ response/@ request/@ controller/@ routes并且它应该可以工作。

+1

谢谢,只是保存了我的培根。与一个名为@request的变量有类似的问题。 – 2011-07-25 21:31:15

+0

哇!与OP有完全相同的代码,现在我的代码与您的解决方案完全相同! @flo – Ben 2014-11-18 05:14:37

2

Flo的答案大多是正确的,但我只是想澄清:

在Rails 3.0.x的是@request有望成为一个ActionController::TestRequest型,其上定义的recycle!方法。有TestResponse,TestRequestTestSession

您可能想要在某些情况下实际初始化或操作这些类型的对象。就我而言,我需要用

@request = ActionController::TestRequest.new unless @request 
@request.host = "www.hostname.com" 
2

我只遇到过类似问题的命名变量@request嘲笑具有特定域名的网站。 我更改为@_request并解决了问题。

+0

谢谢。 '@ response'也是如此。 – scarver2 2015-04-26 21:19:46

相关问题