Rails ActiveSupport: How to assert that an error is raised?

前端 未结 2 1856
别那么骄傲
别那么骄傲 2020-12-15 02:46

I am wanting to test a function on one of my models that throws specific errors. The function looks something like this:

def merge(release_to_delete)
  rais         


        
2条回答
  •  醉梦人生
    2020-12-15 03:28

    To ensure that no exception is raised (or is successfully handled) do inside your test case:

    assert_nothing_raised RuntimeError do
      whatever.merge
    end
    

    To check that error is raised do inside your test case:

    assert_raise RuntimeError do
      whatever.merge
    end
    

    Just a heads up, whatever.merge is the code that raises the error (or doesn't, depending on the assertion type).

提交回复
热议问题