【问题标题】:foo_url(mock_foo) sometimes doesn't work in rspec testsfoo_url(mock_foo) 有时在 rspec 测试中不起作用
【发布时间】:2008-11-21 02:47:24
【问题描述】:

我正在尝试为访问 模型组。

@request.env['HTTP_REFERER'] = group_url(@mock_group) ### 第 49 行

我明白了:

'ActsController 中的 NoMethodError 响应创建应重定向到:back' 当你没想到时,你有一个 nil 对象! 评估 nil.rewrite 时发生错误 /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:621:in `url_for' (eval):17:in `group_url' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/test_process.rb:464:in `send!' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/test_process.rb:464:in `method_missing'

url_for 中的这一行是问题所在;具体来说,@url 为零。

@url.rewrite(rewrite_options(options))

而且好像@url在这里初始化了:

def initialize_current_url @url = UrlRewriter.new(request, params.clone) 结尾

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    发生这种情况是因为 url_for 依赖于在请求处理期间初始化的内容。我假设您的测试看起来像这样:

    it "should do whatever when referrer is group thing" do
      @request.env["HTTP_REFERER"] = url_for(@mock_group)
      get :some_action
      "something".should == "something"
    end
    

    url_for 失败,因为它发生在 get 之前。解决问题的最简单方法是在测试中对 URL 进行硬编码(即将 url\_for(@mock\_group) 更改为 "http://test.host/group/1")。另一种选择是在调用 url_for 之前弄清楚如何让 @controller 初始化 @url。我想我以前做过,但是我没有代码了,这涉及到挖掘 action_controller 的代码。

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多