在Rails Minitest中测试是否从救援块内部调用方法

时间:2020-03-12 05:14:59

标签: ruby-on-rails minitest

我有一个从控制器中调用的模块方法,如下所示:

class MyController
  include MyControllerExtension
end

module MyControllerExtension
  def some_method
    @variable.update!(params)
    respond_to do |format|
      format.html { redirect_to action: 'show' }
    end
  rescue App::CustomErrors::SomeMethodError => e
    errors_catch_method(e)
  end

  def errors_catch_method(error)
    case error
    when App::CustomErrors::SomeMethodError
      formatted_redirect_method 'index'
    when App::CustomErrors::AnotherMethodError
      render action: 'maintenance'
  end

  def formatted_redirect_method(url)
    format.html { redirect_to action: url }
  end
end

现在,每当引发错误并被redirected path块捕获时,我就需要测试rescue。测试代码如下:

MyController::any_instance.stubs(:some_method).raises(App::CustomErrors::SomeMethodError)

每当测试代码到达some_method时,都会引发错误,但不会将其重定向到formatted_redirect_method中的URL。实际上甚至没有errors_catch_method被调用。它仅被引发而不进入rescue块。我应该怎么做才能使minitestrescue块中声明代码?

0 个答案:

没有答案
相关问题