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

前端 未结 2 1877
别那么骄傲
别那么骄傲 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:16

    So unit testing isn't really in activesupport. Ruby comes with a typical xunit framework in the standard libs (Test::Unit in ruby 1.8.x, MiniTest in ruby 1.9), and the stuff in activesupport just adds some stuff to it.

    If you are using Test::Unit/MiniTest

    assert_raise(Exception) { whatever.merge }
    

    if you are using rspec (unfortunately poorly documented, but way more popular)

    lambda { whatever.merge }.should raise_error
    

    If you want to check the raised Exception:

    exception = assert_raises(Exception) { whatever.merge }
    assert_equal( "message", exception.message )
    

提交回复
热议问题