Search code examples
rubyunit-testingrescue

Can I assert inside a begin rescue end block?


I have something that I am testing that I will know its working if it fails. Is there a better way to code this in ruby with test-unit than what I have in my example below?

begin 
  x = Method.shouldFail
  assert_true(false)
rescue Test::Unit::AssertionFailedError
  assert_true(false) #stop test, this is a failure
rescue => e
  assert_equal(400, e.code)
end

This seems very clunky is there a better way to write this? I would expect that Method.shouldFail would always fail but it might not. And I would assume that in the last rescue block e.code should always be 400 but it could be something else.


Solution

  • You can use assert_raise to test that particular exceptions are being thrown.