Rspec 产生了一个未定义的方法 `route_to' ,尽管在 gemfile/rails 助手中定义了 rspec/rails

Rspec producing a undefined method `route_to' , despite having rspec/rails defined in gemfile/rails helper

问题:route_to 方法仍然未定义,我做错了什么?

我对此很陌生,但我正在尝试通过 rspec gem.

开发一些路由测试

我的问题是我遇到了错误:

undefined method `route_to' for #<RSpec::ExampleGroups::RouteToHomepage

我已经查看了此查询的 API,并且我已经完成了以下操作:

Install gem 'rspec-rails'

在rails_helper.rb

    require 'rspec/rails'

在我的routing_spec.rb(我正在写路线的地方)

    require 'rails_helper'

    describe "route to homepage" do
      it "routes /home to index" do
        expect(:get => "/homes").to route_to(
          action: "index"
        )
      end
    end

我到底需要更改或添加什么,所以定义了"route_to"方法?我已经阅读过,显然它是在 "rspec-rails" gem 中定义的,我已经将其包含在内。

来自the documentation

Routing specs are marked by :type => :routing or if you have set config.infer_spec_type_from_file_location! by placing them in spec/routing.


你没有说 routing_spec.rb 的位置,但是 如果 它在文件夹 spec/routing/ 中,那么你可以选择启用上述配置选项。

否则,或者一般来说,您必须这样做:

require 'rails_helper'

describe "route to homepage", type: :routing do
  it "routes /home to index" do
    expect(:get => "/homes").to route_to(
      action: "index"
    )
  end
end

这样做将包括定义 route_to 的必要 RSpec 帮助程序,以及其他方法。