2016-05-23 93 views
3
  • Rails的5.0.0.rc1
  • rspec的核@ a99ff26
  • RSpec的护栏@ 052206f

我有一个控制器规范,看起来大致像这样:RSpec的控制器规范行事请求等规范

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 

/这个生活在规格/控制器inquiries_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从机架到来。不知道为什么。这里是我的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住在应用程序/控制器/ inquiries_controller.rb,是不是在模块下面,并且路由正确连接(我通过手动访问并验证它来验证作品)。该方法确实名为create,并且我已经成功完成了其他项目。

+1

可能值得注意的是,将':create'更改为'/ queries'可以使规范通过。 –

+0

48小时后您可以接受您自己的答案。见http://stackoverflow.com/help/self-answer –

+0

@HolgerJust我知道,但我想避免人们浪费他们的时间看这:) –

回答

0

我找出发生了什么事。我无意中复制了包括Rack::Test::Methods的公司标准spec_helper,将Rack方法注入我的所有测试中。