Rails 伪造路线

发布于 2024-09-13 13:27:30 字数 170 浏览 2 评论 0原文

具体来说,我试图让 ActionController::Routing::Routes.recognize_path 识别不在 routes.rb 中的路由,以用于测试目的。

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

To be specific, I'm trying to get ActionController::Routing::Routes.recognize_path to recognize a route that is not in routes.rb, for testing purposes.

Is it possible to somehow mock or dynamically add a route? I'm using Rspec with Mocha.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初相遇 2024-09-20 13:27:30

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

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

I have absolutely no idea whether will work but you could experiment with something like this:

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
奈何桥上唱咆哮 2024-09-20 13:27:30

fakeweb gem 位于 http://github.com/chrisk/fakeweb可能适合您的需求。

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

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 被绕过,并返回真实请求的响应

The fakeweb gem at http://github.com/chrisk/fakeweb might fit your needs.

How to register a basic string response (from the README):

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

To test:

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

returns "Hello World!"

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

In this case, FakeWeb is bypassed and the response from a real request is returned

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文