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
,並且我已經成功完成了其他項目。
可能值得注意的是,將':create'更改爲'/ queries'可以使規範通過。 –
48小時後您可以接受您自己的答案。見http://stackoverflow.com/help/self-answer –
@HolgerJust我知道,但我想避免人們浪費他們的時間看這:) –