2010-08-09 84 views
1

具体而言,我试图让ActionController::Routing::Routes.recognize_path识别不在routes.rb中的路线,以便进行测试。Rails Faking a Route

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

回答

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 
+0

谢谢,'ActionController :: Routing :: Routes.draw'就是我一直在寻找的东西。 – Karl 2010-08-10 17:30:27

0

fakeweb宝石在http://github.com/chrisk/fakeweb可能适合您的需求。

如何注册(自述)基本字符串响应:

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

要测试:

​​

返回"Hello World!"

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

在这种情况下,FakeWeb被旁路并返回来自真实请求的回复