Throws
The Throws
method is used to throw an exception when a given call is made. This topic goes through a number of scenarios where the Throws
method is useful.
Here is the system under test for these examples:
Throw Exception on Method Call
Change a method behavior to throw an exception once it is called.
The assert step is done via the ExpectedException
attribute, where we explicitly specify that an exception of type ArgumentException
must be thrown during the execution of the test.
Throw Exception with Arguments on Method Call
Change a method behavior to throw an exception once it is called and pass arguments to the exception.
The assert step is done via the ExpectedException
attribute, where we explicitly specify that an exception of type ArgumentException
must be thrown during the execution of the test. Calling foo.Execute
with empty string will result in throwing an exception and passing "Argument shouldn't be empty." to it.