1

具体来说,出于测试目的,我正在尝试ActionController::Routing::Routes.recognize_path识别不在的路线。routes.rb

是否可以以某种方式模拟或动态添加路线?我正在将 Rspec 与 Mocha 一起使用。

4

2 回答 2

3

我完全不知道是否会起作用,但你可以尝试这样的事情:

class ApplicationController < ActionController::Base

  rescue_from ActionView::MissingTemplate do |exception|
    # use exception.path to extract the path information
    ActionController::Routing::Routes.draw do |map|
      # Add your dynamic route using path here and then do a redirect to it
    end
  end

end
于 2010-08-10T00:03:49.567 回答
0

http://github.com/chrisk/fakeweb上的fakewebgem可能适合您的需求。

如何注册一个基本的字符串响应(来自自述文件):

FakeWeb.register_uri(:get, "http://example.com/test1", :body => "Hello World!")

去测试:

Net::HTTP.get(URI.parse("http://example.com/test1"))

返回"Hello World!"

Net::HTTP.get(URI.parse("http://example.com/test2"))

在这种情况下,会绕过 FakeWeb 并返回来自真实请求的响应

于 2010-08-10T02:31:35.003 回答