【问题标题】:Access the routes from helpers Rspec for engine从助手 Rspec 访问引擎的路由
【发布时间】:2016-12-12 19:49:53
【问题描述】:

我正在尝试从引擎的助手 rspec 访问路由 尝试了一些东西 expect(:get => "routes_path").to route_to(:controller => "name")"。

但因 NoMethodError 失败:未定义的方法 `routes_path

经过大量搜索,我知道可以通过控制器访问路由并进行测试。但是也可以通过助手来完成吗?

【问题讨论】:

  • routes_path 是您可以从典型的帮助上下文(即视图)访问的东西吗?
  • 你为什么会有一个叫routes_path的东西?你有一个叫/routes的端点吗?
  • 是的……我有一个端点.. /routes routes_path 给出了 /routes 路径,但是如果路由正确,如何在帮助程序中进行测试。我有一个替代选项,但测试路径`allow(helper).to receive(:routes).and_return(routes)`
  • 然后测试路径,如果两条路径相同。但我想知道它是否可以测试routes

标签: ruby ruby-on-rails-4 rspec rspec2 rspec3


【解决方案1】:

在你的 Rspec 配置中有这个可能会起作用

RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
  ...
end

您可以查看this问题。

您也可以尝试使用实际路径和控制器#action。

类似:

expect(get("path/to/routes")).to route_to("your_controller#action")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    相关资源
    最近更新 更多