[1146] | 1 | <!-- saved from url=(0014)about:internet --><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
|
---|
| 2 | <html>
|
---|
| 3 | <!-- Standard Head Part -->
|
---|
| 4 | <head>
|
---|
| 5 | <title>NUnit - ExceptionAsserts</title>
|
---|
| 6 | <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
---|
| 7 | <meta http-equiv="Content-Language" content="en-US">
|
---|
| 8 | <link rel="stylesheet" type="text/css" href="nunit.css">
|
---|
| 9 | <link rel="shortcut icon" href="favicon.ico">
|
---|
| 10 | </head>
|
---|
| 11 | <!-- End Standard Head Part -->
|
---|
| 12 |
|
---|
| 13 | <body>
|
---|
| 14 |
|
---|
| 15 | <!-- Standard Header for NUnit.org -->
|
---|
| 16 | <div id="header">
|
---|
| 17 | <a id="logo" href="http://www.nunit.org"><img src="img/logo.gif" alt="NUnit.org" title="NUnit.org"></a>
|
---|
| 18 | <div id="nav">
|
---|
| 19 | <a href="http://www.nunit.org">NUnit</a>
|
---|
| 20 | <a class="active" href="index.html">Documentation</a>
|
---|
| 21 | </div>
|
---|
| 22 | </div>
|
---|
| 23 | <!-- End of Header -->
|
---|
| 24 |
|
---|
| 25 | <div id="content">
|
---|
| 26 |
|
---|
| 27 | <h2>Exception Asserts (NUnit 2.5)</h2>
|
---|
| 28 |
|
---|
| 29 | <p>The <b>Assert.Throws</b> method is pretty much in a class by itself. Rather than
|
---|
| 30 | comparing values, it attempts to invoke a code snippet, represented as
|
---|
| 31 | a delegate, in order to verify that it throws a particular exception.
|
---|
| 32 |
|
---|
| 33 | <p>It's also in a class by itself in that it returns an Exception, rather
|
---|
| 34 | than void, if the Assert is successful. See the example below for
|
---|
| 35 | a few ways to use this.
|
---|
| 36 |
|
---|
| 37 | <p><b>Assert.Throws</b> may be used with a constraint argument, which is applied
|
---|
| 38 | to the actual exception thrown, or with the Type of exception expected.
|
---|
| 39 | The Type format is available in both both a non-generic and (in the .NET 2.0 version)
|
---|
| 40 | generic form.
|
---|
| 41 |
|
---|
| 42 | <p><b>Assert.DoesNotThrow</b> simply verifies that the delegate does not throw
|
---|
| 43 | an exception.
|
---|
| 44 |
|
---|
| 45 | <p><b>Assert.Catch</b> is similar to <b>Assert.Throws</b> but will pass for an exception
|
---|
| 46 | that is derived from the one specified.
|
---|
| 47 |
|
---|
| 48 | <div class="code" style="width: 40em"><pre>
|
---|
| 49 | Exception Assert.Throws( Type expectedExceptionType, TestDelegate code );
|
---|
| 50 | Exception Assert.Throws( Type expectedExceptionType, TestDelegate code,
|
---|
| 51 | string message );
|
---|
| 52 | Exception Assert.Throws( Type expectedExceptionType, TestDelegate code,
|
---|
| 53 | string message, params object[] parms);
|
---|
| 54 |
|
---|
| 55 | Exception Assert.Throws( IResolveConstraint constraint, TestDelegate code );
|
---|
| 56 | Exception Assert.Throws( IResolveConstraint constraint, TestDelegate code,
|
---|
| 57 | string message );
|
---|
| 58 | Exception Assert.Throws( IResolveConstraint constraint, TestDelegate code,
|
---|
| 59 | string message, params object[] parms);
|
---|
| 60 |
|
---|
| 61 | T Assert.Throws<T>( TestDelegate code );
|
---|
| 62 | T Assert.Throws<T>( TestDelegate code, string message );
|
---|
| 63 | T Assert.Throws<T>( TestDelegate code, string message,
|
---|
| 64 | params object[] parms);
|
---|
| 65 |
|
---|
| 66 | void Assert.DoesNotThrow( TestDelegate code );
|
---|
| 67 | void Assert.DoesNotThrow( TestDelegate code, string message );
|
---|
| 68 | void Assert.DoesNotThrow( TestDelegate code, string message,
|
---|
| 69 | params object[] parms);
|
---|
| 70 |
|
---|
| 71 | Exception Assert.Catch( TestDelegate code );
|
---|
| 72 | Exception Assert.Catch( TestDelegate code, string message );
|
---|
| 73 | Exception Assert.Catch( TestDelegate code, string message,
|
---|
| 74 | params object[] parms);
|
---|
| 75 |
|
---|
| 76 | Exception Assert.Catch( Type expectedExceptionType, TestDelegate code );
|
---|
| 77 | Exception Assert.Catch( Type expectedExceptionType, TestDelegate code,
|
---|
| 78 | string message );
|
---|
| 79 | Exception Assert.Catch( Type expectedExceptionType, TestDelegate code,
|
---|
| 80 | string message, params object[] parms);
|
---|
| 81 |
|
---|
| 82 | T Assert.Catch<T>( TestDelegate code );
|
---|
| 83 | T Assert.Catch<T>( TestDelegate code, string message );
|
---|
| 84 | T Assert.Catch<T>( TestDelegate code, string message,
|
---|
| 85 | params object[] parms);
|
---|
| 86 | </pre></div>
|
---|
| 87 |
|
---|
| 88 | <p>In the above code <b>TestDelegate</b> is a delegate of the form
|
---|
| 89 | <b>void TestDelegate()</b>, which is used to execute the code
|
---|
| 90 | in question. Under .NET 2.0, this may be an anonymous delegate.
|
---|
| 91 | If compiling under C# 3.0, it may be a lambda expression.
|
---|
| 92 |
|
---|
| 93 | <p>The following example shows different ways of writing the
|
---|
| 94 | same test.
|
---|
| 95 |
|
---|
| 96 | <div class="code"><pre>
|
---|
| 97 | [TestFixture]
|
---|
| 98 | public class AssertThrowsTests
|
---|
| 99 | {
|
---|
| 100 | [Test]
|
---|
| 101 | public void Tests()
|
---|
| 102 | {
|
---|
| 103 | // .NET 1.x
|
---|
| 104 | Assert.Throws( typeof(ArgumentException),
|
---|
| 105 | new TestDelegate(MethodThatThrows) );
|
---|
| 106 |
|
---|
| 107 | // .NET 2.0
|
---|
| 108 | Assert.Throws<ArgumentException>( MethodThatThrows() );
|
---|
| 109 |
|
---|
| 110 | Assert.Throws<ArgumentException>(
|
---|
| 111 | delegate { throw new ArgumentException(); } );
|
---|
| 112 |
|
---|
| 113 | // Using C# 3.0
|
---|
| 114 | Assert.Throws<ArgumentException>(
|
---|
| 115 | () => throw new ArgumentException(); } );
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | void MethodThatThrows()
|
---|
| 119 | {
|
---|
| 120 | throw new ArgumentException();
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | </pre></div>
|
---|
| 124 |
|
---|
| 125 | <p>This example shows use of the return value to perform
|
---|
| 126 | additional verification of the exception.
|
---|
| 127 |
|
---|
| 128 | <div class="code"><pre>
|
---|
| 129 | [TestFixture]
|
---|
| 130 | public class UsingReturnValue
|
---|
| 131 | {
|
---|
| 132 | [Test]
|
---|
| 133 | public void TestException()
|
---|
| 134 | {
|
---|
| 135 | MyException ex = Assert.Throws<MyException>(
|
---|
| 136 | delegate { throw new MyException( "message", 42 ); } );
|
---|
| 137 | Assert.That( ex.Message, Is.EqualTo( "message" ) );
|
---|
| 138 | Assert.That( ex.MyParam, Is.EqualTo( 42 ) );
|
---|
| 139 | }
|
---|
| 140 | </pre></div>
|
---|
| 141 |
|
---|
| 142 | <p>This example does the same thing
|
---|
| 143 | using the overload that includes a constraint.
|
---|
| 144 |
|
---|
| 145 | <div class="code"><pre>
|
---|
| 146 | [TestFixture]
|
---|
| 147 | public class UsingConstraint
|
---|
| 148 | {
|
---|
| 149 | [Test]
|
---|
| 150 | public void TestException()
|
---|
| 151 | {
|
---|
| 152 | Assert.Throws( Is.Typeof<MyException>()
|
---|
| 153 | .And.Message.EqualTo( "message" )
|
---|
| 154 | .And.Property( "MyParam" ).EqualTo( 42 ),
|
---|
| 155 | delegate { throw new MyException( "message", 42 ); } );
|
---|
| 156 | }
|
---|
| 157 | </pre></div>
|
---|
| 158 |
|
---|
| 159 | <p>Use the form that matches your style of coding.
|
---|
| 160 |
|
---|
| 161 | <h3>Exact Versus Derived Types</h3>
|
---|
| 162 |
|
---|
| 163 | <p>When used with a Type argument, <b>Assert.Throws</b> requires
|
---|
| 164 | that exact type to be thrown. If you want to test for any
|
---|
| 165 | derived Type, use one of the forms that allows specifying
|
---|
| 166 | a constraint. Alternatively, you may use <b>Assert.Catch</b>,
|
---|
| 167 | which differs from <b>Assert.Throws</b> in allowing derived
|
---|
| 168 | types. See the following code for examples:
|
---|
| 169 |
|
---|
| 170 | <div class="code">
|
---|
| 171 | <pre>
|
---|
| 172 | // Require an ApplicationException - derived types fail!
|
---|
| 173 | Assert.Throws( typeof(ApplicationException), code );
|
---|
| 174 | Assert.Throws<ApplicationException>()( code );
|
---|
| 175 |
|
---|
| 176 | // Allow both ApplicationException and any derived type
|
---|
| 177 | Assert.Throws( Is.InstanceOf( typeof(ApplicationException), code );
|
---|
| 178 | Assert.Throws( Is.InstanceOf<ApplicationException>(), code );
|
---|
| 179 |
|
---|
| 180 | // Allow both ApplicationException and any derived type
|
---|
| 181 | Assert.Catch<ApplicationException>( code );
|
---|
| 182 |
|
---|
| 183 | // Allow any kind of exception
|
---|
| 184 | Assert.Catch( code );
|
---|
| 185 | </pre>
|
---|
| 186 | </div>
|
---|
| 187 |
|
---|
| 188 | <h4>See also...</h4>
|
---|
| 189 | <ul>
|
---|
| 190 | <li><a href="throwsConstraint.html">ThrowsConstraint</a></ul>
|
---|
| 191 |
|
---|
| 192 | </div>
|
---|
| 193 |
|
---|
| 194 | <!-- Submenu -->
|
---|
| 195 | <div id="subnav">
|
---|
| 196 | <ul>
|
---|
| 197 | <li><a href="index.html">NUnit 2.5.10</a></li>
|
---|
| 198 | <ul>
|
---|
| 199 | <li><a href="getStarted.html">Getting Started</a></li>
|
---|
| 200 | <li><a href="assertions.html">Assertions</a></li>
|
---|
| 201 | <ul>
|
---|
| 202 | <li><a href="equalityAsserts.html">Equality Asserts</a></li>
|
---|
| 203 | <li><a href="identityAsserts.html">Identity Asserts</a></li>
|
---|
| 204 | <li><a href="conditionAsserts.html">Condition Asserts</a></li>
|
---|
| 205 | <li><a href="comparisonAsserts.html">Comparison Asserts</a></li>
|
---|
| 206 | <li><a href="typeAsserts.html">Type Asserts</a></li>
|
---|
| 207 | <li id="current"><a href="exceptionAsserts.html">Exception Asserts</a></li>
|
---|
| 208 | <li><a href="utilityAsserts.html">Utility Methods</a></li>
|
---|
| 209 | <li><a href="stringAssert.html">String Assert</a></li>
|
---|
| 210 | <li><a href="collectionAssert.html">Collection Assert</a></li>
|
---|
| 211 | <li><a href="fileAssert.html">File Assert</a></li>
|
---|
| 212 | <li><a href="directoryAssert.html">Directory Assert</a></li>
|
---|
| 213 | </ul>
|
---|
| 214 | <li><a href="constraintModel.html">Constraints</a></li>
|
---|
| 215 | <li><a href="attributes.html">Attributes</a></li>
|
---|
| 216 | <li><a href="runningTests.html">Running Tests</a></li>
|
---|
| 217 | <li><a href="extensibility.html">Extensibility</a></li>
|
---|
| 218 | <li><a href="releaseNotes.html">Release Notes</a></li>
|
---|
| 219 | <li><a href="samples.html">Samples</a></li>
|
---|
| 220 | <li><a href="license.html">License</a></li>
|
---|
| 221 | </ul>
|
---|
| 222 | </ul>
|
---|
| 223 | </div>
|
---|
| 224 | <!-- End of Submenu -->
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | <!-- Standard Footer for NUnit.org -->
|
---|
| 228 | <div id="footer">
|
---|
| 229 | Copyright © 2010 Charlie Poole. All Rights Reserved.
|
---|
| 230 | </div>
|
---|
| 231 | <!-- End of Footer -->
|
---|
| 232 |
|
---|
| 233 | </body>
|
---|
| 234 | </html>
|
---|