3

In my Rails application I am doing a unit test where I expect an error when calling a certain method.

test "sending a logout request while not logged should not return an error" do
  assert_nothing_raised do
    do_a_logout
  end
end

Problem is that the test keeps reporting as Error instead as of Failure when do_a_logout errors out.

The error is NoMethodError: NoMethodError: undefined method 'forget' for nil:NilClass

It comes from this method two or three levels below the do_a_logout call

def forget user
  user.forget
  cookies.delete :user_id
  cookies.delete :remember_token
end

Here, the error I am expecting is for user to be nil and fail when calling forget. How can I make the test report a Failure instead of an Error?

William Troy
  • 98
  • 1
  • 7
  • Could you include the code for the `do_a_logout` method in your question, there might actually be an error there. – Rob Aug 05 '15 at 16:15
  • And also please provide full error message which you see. – EugZol Aug 05 '15 at 16:16
  • 1
    It's logical that the error is raised, as you say you are expecting an exception so the exception is raised, during application execution therefore execution is terminated – mr. Holiday Aug 05 '15 at 16:34

2 Answers2

5

You want to use assert_raise with the exception class you're expecting:

assert_raise(NoMethodError) { do_a_logout }

ETA: If you want to test that no error was raised, you can use assert_nothing_raised:

assert_nothing_raised { do_a_logout }
assert_nothing_raised(NoMethodError) { do_a_logout }

However as pointed out on SO and elsewhere, there is no good reason to use this assertion, because its redundant: you never expect an error to be raised, unless you specify otherwise.

Moreover, it gives a less helpful failure than simply running the spec without the assertion. In particular, running with assert_nothing_raised when an error occurs will give you a backtrace only to the assert_nothing_raised call, while running the spec without this assertion will give a backtrace to the actual error that's occurring.

Community
  • 1
  • 1
eirikir
  • 3,802
  • 3
  • 21
  • 39
1

based on @nus comment,

test "sending a logout request while not logged should not return an error" do
  do_a_logout
  # assert true # optional
end

or

assert_silent { do_a_logout }
Warren Le
  • 155
  • 1
  • 6