我嘗試測試我的RoR應用程序。當我測試造成的ActionController :: RoutingError在我的控制器我獲得以下錯誤: 了1個衰竭等1個錯誤:預計ActionController :: RoutingError,但沒有給出在rails應用程序中的塊rspec
2.1) Failure/Error: expect(visit meters_path).to raise_error(ActionController::RoutingError)
expected ActionController::RoutingError but was not given a block
# ./spec/integration/meter_behavior_spec.rb:70:in `block (2 levels) in <top (required)>'
2.2) Failure/Error: raise ActionController::RoutingError.new('Not Found')
ActionController::RoutingError:
Not Found
的原因是什麼? 這是我的測試寶石列表:
group :development, :test do
gem 'factory_girl_rails'
gem 'faker'
gem 'rspec-rails'
end
group :test do
gem 'capybara'
gem 'database_cleaner'
gem 'launchy'
gem 'selenium-webdriver', '2.53.4'
gem 'capybara-webkit'
gem 'shoulda-matchers', '~> 3.1'
end
我的規格文件
meter_behavior_spec.rb
scenario 'a visitor cannot to view meters pages', driver: :webkit do
user = FactoryGirl.create(:client1)
user.meters.create(attributes_for(:meter))
expect(visit meters_path).to raise_error(ActionController::RoutingError)
end
meters_controller.rb:
before_action only: :index do
access_allowed_for(['Client'])
end
# GET /meters
# GET /meters.json
def index
respond_to do |format|
format.json { render json: MetersDatatable.new(view_context, current_user) }
format.html
end
end
application_controller.rb:
def access_allowed_for(statuses_ary=[])
statuses_ary << 'Admin'
if !user_signed_in? || statuses_ary.exclude?(current_user.try(:status))
raise ActionController::RoutingError.new('Not Found')
end
end
1我檢查了我的'web-console'和'更好的錯誤'gem在我的gemfile中,但都在'development'組中。 2然後我將'Capybara.raise_server_errors = true'字符串添加到'spec/support/capybara.rb'文件中,但又得到了錯誤。 3最後,我添加了'page.has_text?'語句,它有所幫助。謝謝。 –
當我評論'Capybara.raise_server_errors = true'字符串時,測試一切正常。 –
@ЭрикКеубасов是的 - 它默認爲true,我只是想確保你沒有將它重置爲false。 –