【问题标题】:Method get of ActionController::TestCase ignores routes.rb?ActionController::TestCase 的方法 get 忽略 routes.rb?
【发布时间】:2011-09-05 14:26:12
【问题描述】:

我遇到的问题似乎表明 ActionController::TestCase.get() 方法忽略了我在 routes.rb 中的内容。

Rails 版本是 3.0.10。

我对我的 XmlRpcController#index 操作进行了以下 RSpec2 测试:

it "should get nothing in response to GET request" do
  get :index
  response.response_code.should == 400 #bad_request
end

并且在 routes.rb 中与这条路线相关的唯一一行是:

post 'rpc', :to => "xml_rpc#index"

'rake routes' 也仅显示已定义的此路由。

因此,当我运行此测试时,该操作实际上确实被执行了!我通过在其中放置一个简单的 puts 来判断这一点)并且还有一个日志包含:

XmlRpcController#index 作为 HTML 处理

另外,如果我在浏览器中转到“localhost:3000/rpc” - 它说没有找到路由:就像它应该的那样。但是测试有其他行为,这让我很困惑......

谁能暗示我为什么会这样?我才刚刚开始学习 RoR :) 早些时候,在我看来,TestCase 的这些“get/post”方法确实尊重 routes.rb...

我是否遗漏了一些明显的东西? :)

【问题讨论】:

    标签: ruby-on-rails unit-testing controller


    【解决方案1】:

    似乎 'get :index' 方法确实忽略了 routes.rb。

    对我来说真正的解决方案是使用 be_routeable 为这个特定目的编写的 rspec 匹配器:

      describe "GET 'contact'" do
        it "should be successful" do
          { :get => '/rpc' }.should_not be_routable
        end
      end
    

    感谢来自 Ruby-Forum 的一些用户。更多信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 2012-08-05
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      相关资源
      最近更新 更多