最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ruby on rails - Rspec: testing "rescue_from" on type: :request - Stack Overflow

programmeradmin4浏览0评论

I have a Rails 6.1 controller that sets:

# File: app/controllers/foo_controller.rb
rescue_from Exception, with: :error_handler

My Rspec 6.1 test (is a "request" type) is like:

 # File: spec/requests/foo_controller_spec.rb
 expect do
   post foo_path, params: wrong_params
 end.to raise_error(Exception, 'Non valid params') 

That type of tests don't work because "rescue_from" blocks the exception call. I see there is the bypass_rescue option, but it works only for the Rspec controller type test. I tried adding to my test:

# File: spec/requests/foo_controller_spec.rb
module BypassRescue
  def rescue_with_handler(exception)
    raise exception
  end
end

# Extends the controller with a module that overrides
# `rescue_with_handler` to raise the exception passed to it. Use this to
# specify that an action _should_ raise an exception given appropriate
# conditions.

def bypass_rescue
  controller.extend(BypassRescue)
end

And then:

# File: spec/requests/foo_controller_spec.rb
expect do
  bypass_rescue
  post foo_path, params: wrong_params
end.to raise_error(Exception, 'Non valid params')  

But it didn't work. So, my question is, should I abandon the "raise_error(Exception" option? or there is way to test the exception dodging the "rescue_from" method?

I have a Rails 6.1 controller that sets:

# File: app/controllers/foo_controller.rb
rescue_from Exception, with: :error_handler

My Rspec 6.1 test (is a "request" type) is like:

 # File: spec/requests/foo_controller_spec.rb
 expect do
   post foo_path, params: wrong_params
 end.to raise_error(Exception, 'Non valid params') 

That type of tests don't work because "rescue_from" blocks the exception call. I see there is the bypass_rescue option, but it works only for the Rspec controller type test. I tried adding to my test:

# File: spec/requests/foo_controller_spec.rb
module BypassRescue
  def rescue_with_handler(exception)
    raise exception
  end
end

# Extends the controller with a module that overrides
# `rescue_with_handler` to raise the exception passed to it. Use this to
# specify that an action _should_ raise an exception given appropriate
# conditions.

def bypass_rescue
  controller.extend(BypassRescue)
end

And then:

# File: spec/requests/foo_controller_spec.rb
expect do
  bypass_rescue
  post foo_path, params: wrong_params
end.to raise_error(Exception, 'Non valid params')  

But it didn't work. So, my question is, should I abandon the "raise_error(Exception" option? or there is way to test the exception dodging the "rescue_from" method?

Share Improve this question edited Feb 14 at 0:18 aarkerio asked Feb 14 at 0:11 aarkerioaarkerio 2,3642 gold badges22 silver badges38 bronze badges 1
  • 1 What you're doing here is an anti-pattern known as Pokemon exception handling (gotta catch them all). You should never rescue Exception as it's the parent class of all exceptions including stuff like SignalException::Interrupt which you should not prevent. Only rescue specific exception classes that you know what to do with and leave the high level exception handling on the framework level. This code will just bury bugs and make debugging exceptionally hard. Get rid of it. – max Commented Feb 14 at 7:23
Add a comment  | 

1 Answer 1

Reset to default 0

This works:

# File: spec/requests/foo_controller_spec.rb
module ActionController
  module Rescue
    def rescue_with_handler(exception)
      raise exception
    end
  end
end

describe FooController, type: :request do
  describe '#raise_exception' do
    it 'is the payload is not a valid one' do
      expect do
        post inbound_messages_provider_path(:bandwidth), params: { foo: :bar }, headers: @headers
  end.to raise_error(Exception, 'Non valid BW params. Spam.')
     end
  end
end
发布评论

评论列表(0)

  1. 暂无评论