代码之家  ›  专栏  ›  技术社区  ›  Karl

伪造路线的铁路

  •  1
  • Karl  · 技术社区  · 15 年前

    ActionController::Routing::Routes.recognize_path 识别不在 routes.rb

    是否可以以某种方式模拟或动态添加路由?我在用Rspec和摩卡。

    2 回复  |  直到 15 年前
        1
  •  3
  •   bjg    15 年前

    我完全不知道这样做是否有效,但你可以这样做:

    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
    
        2
  •  0
  •   Bennett    15 年前

    这个 fakeweb 宝石 http://github.com/chrisk/fakeweb 可能适合你的需要。

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

    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"))
    

    推荐文章