繁体   English   中英

RSpec控制器规范的作用类似于请求规范

[英]RSpec controller spec is acting like a request spec

  • Rails 5.0.0.rc1
  • rspec的核@ a99ff26
  • RSpec的护栏@ 052206f

我有一个看起来像这样的控制器规格:

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 / queries_controller_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来自Rack。 不知道为什么。 这是我的spec_helper和rails_helper:

spec_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

rails_helper.rb

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 / queries_controller.rb中 ,不在模块下面,并且路由正确连接(我通过手动访问它并验证它是否有效来验证这一点)。 该方法确实名为create ,我已成功完成了其他项目。

我发现了发生了什么。 我盲目地复制了一个包含Rack::Test::Methods的公司标准spec_helper ,将Rack::Test::Methods注入我的所有测试中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM