1

I unity I have a function to call with wrong arguments and I want to make sure that the function throw the right exception with the right message. So my function call is this:

winDetector.DoMove(move)

And it should throw and exception like this:

throw new Exception("Move is not valid.");

Looks like I should use Assert.Throws<Exception> but I don't know how. Now to both Unity and C#. How can I do this?

P.S. Assert.Throws<Exception> is not the correct way. Please see my answer below.

Narek
  • 36,981
  • 76
  • 218
  • 375
  • 2
    Possible duplicate of [How do I use Assert to verify that an exception has been thrown?](https://stackoverflow.com/questions/933613/how-do-i-use-assert-to-verify-that-an-exception-has-been-thrown) – scharette Jun 12 '18 at 12:31
  • 2
    This is not a duplicate. See the answer below. – Narek Jun 13 '18 at 05:58

1 Answers1

4

There is an elegant way to do that. Here is how it should be done:

Assert.That(() => winDetector.DoMove(move), 
                  Throws.TypeOf<Exception>());
Narek
  • 36,981
  • 76
  • 218
  • 375