Ruby on rails RSpec控制器规范的作用类似于请求规范 轨道5.0.0.rc1 rspec-core@a99ff26 rspec-rails@052206f

Ruby on rails RSpec控制器规范的作用类似于请求规范 轨道5.0.0.rc1 rspec-core@a99ff26 rspec-rails@052206f,ruby-on-rails,rspec,ruby-on-rails-5,Ruby On Rails,Rspec,Ruby On Rails 5,我有一个大致如下的控制器规范: require "rails_helper" RSpec.describe InquiriesController, type: :controller do describe "#create" do let(:inquiry_attributes) { attributes_for(:inquiry) } before do post :create, inquiry: inquiry_attributes end

我有一个大致如下的控制器规范:

require "rails_helper"

RSpec.describe InquiriesController, type: :controller do
  describe "#create" do
    let(:inquiry_attributes) { attributes_for(:inquiry) }

    before do
      post :create, inquiry: inquiry_attributes
    end

    it "whatever" do
      expect(2).to be 2
    end
  end
end
这存在于spec/controllers/inquiries\u controller\u spec.rb中,因此RSpec不应该根据其位置推断它是请求规范

运行此规范时,出现以下错误:

1) InquiriesController#create whatever
   Failure/Error: post :create, inquiry: inquiry_attributes

   URI::InvalidURIError:
     bad URI(is not URI?): create
     # /Users/spetryk/.gem/ruby/2.3.1/gems/rack-test-0.6.3/lib/rack/test.rb:193:in `env_for'
     # /Users/spetryk/.gem/ruby/2.3.1/gems/rack-test-0.6.3/lib/rack/test.rb:66:in `post'
嗯,看来
post
来自机架。不知道为什么。这是我的spec\u助手和rails\u助手:

spec\u helper.rb

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.filter_run focus: true
  config.run_all_when_everything_filtered = true
end
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?

require "spec_helper"
require "rspec/rails"
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.use_transactional_fixtures = true

  # Yes, I've tried removing this line
  config.infer_spec_type_from_file_location!

  config.filter_rails_from_backtrace!

  config.include Rails.application.routes.url_helpers
  config.include FactoryGirl::Syntax::Methods
end
rails\u helper.rb

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.filter_run focus: true
  config.run_all_when_everything_filtered = true
end
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?

require "spec_helper"
require "rspec/rails"
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.use_transactional_fixtures = true

  # Yes, I've tried removing this line
  config.infer_spec_type_from_file_location!

  config.filter_rails_from_backtrace!

  config.include Rails.application.routes.url_helpers
  config.include FactoryGirl::Syntax::Methods
end

我的InquiriesController位于app/controllers/inquiries\u controller.rb中,不在模块下,并且路由连接正确(我通过手动访问并验证它是否工作来验证这一点)。该方法的名称确实是
create
,我在其他项目中也成功地做到了这一点。

我发现了发生了什么。我无意中复制了一个公司标准
spec\u helper
,其中包括
Rack::Test::Methods
,将Rack方法注入到我的所有测试中。

可能值得注意的是,将
:create
更改为
/inquiries
会使规范通过。48小时后你可以接受自己的答案。看@HolgerJust我知道,但我想避免人们浪费时间看这个:)