source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/lib/NUnit/NUnit-2.5.10.11092/doc/exceptionAsserts.html@ 1146

Last change on this file since 1146 was 1146, checked in by Sam Habiel, 13 years ago

Initial Import of BMX4

File size: 7.9 KB
Line 
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>
49Exception Assert.Throws( Type expectedExceptionType, TestDelegate code );
50Exception Assert.Throws( Type expectedExceptionType, TestDelegate code,
51 string message );
52Exception Assert.Throws( Type expectedExceptionType, TestDelegate code,
53 string message, params object[] parms);
54
55Exception Assert.Throws( IResolveConstraint constraint, TestDelegate code );
56Exception Assert.Throws( IResolveConstraint constraint, TestDelegate code,
57 string message );
58Exception Assert.Throws( IResolveConstraint constraint, TestDelegate code,
59 string message, params object[] parms);
60
61T Assert.Throws&lt;T&gt;( TestDelegate code );
62T Assert.Throws&lt;T&gt;( TestDelegate code, string message );
63T Assert.Throws&lt;T&gt;( TestDelegate code, string message,
64 params object[] parms);
65
66void Assert.DoesNotThrow( TestDelegate code );
67void Assert.DoesNotThrow( TestDelegate code, string message );
68void Assert.DoesNotThrow( TestDelegate code, string message,
69 params object[] parms);
70
71Exception Assert.Catch( TestDelegate code );
72Exception Assert.Catch( TestDelegate code, string message );
73Exception Assert.Catch( TestDelegate code, string message,
74 params object[] parms);
75
76Exception Assert.Catch( Type expectedExceptionType, TestDelegate code );
77Exception Assert.Catch( Type expectedExceptionType, TestDelegate code,
78 string message );
79Exception Assert.Catch( Type expectedExceptionType, TestDelegate code,
80 string message, params object[] parms);
81
82T Assert.Catch&lt;T&gt;( TestDelegate code );
83T Assert.Catch&lt;T&gt;( TestDelegate code, string message );
84T Assert.Catch&lt;T&gt;( 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
90in question. Under .NET 2.0, this may be an anonymous delegate.
91If compiling under C# 3.0, it may be a lambda expression.
92
93<p>The following example shows different ways of writing the
94same test.
95
96<div class="code"><pre>
97[TestFixture]
98public 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&lt;ArgumentException&gt;( MethodThatThrows() );
109
110 Assert.Throws&lt;ArgumentException&gt;(
111 delegate { throw new ArgumentException(); } );
112
113 // Using C# 3.0
114 Assert.Throws&lt;ArgumentException&gt;(
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
126additional verification of the exception.
127
128<div class="code"><pre>
129[TestFixture]
130public class UsingReturnValue
131{
132 [Test]
133 public void TestException()
134 {
135 MyException ex = Assert.Throws&lt;MyException&gt;(
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
143using the overload that includes a constraint.
144
145<div class="code"><pre>
146[TestFixture]
147public class UsingConstraint
148{
149 [Test]
150 public void TestException()
151 {
152 Assert.Throws( Is.Typeof&lt;MyException&gt;()
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
164that exact type to be thrown. If you want to test for any
165derived Type, use one of the forms that allows specifying
166a constraint. Alternatively, you may use <b>Assert.Catch</b>,
167which differs from <b>Assert.Throws</b> in allowing derived
168types. See the following code for examples:
169
170<div class="code">
171<pre>
172// Require an ApplicationException - derived types fail!
173Assert.Throws( typeof(ApplicationException), code );
174Assert.Throws&lt;ApplicationException&gt;()( code );
175
176// Allow both ApplicationException and any derived type
177Assert.Throws( Is.InstanceOf( typeof(ApplicationException), code );
178Assert.Throws( Is.InstanceOf&lt;ApplicationException&gt;(), code );
179
180// Allow both ApplicationException and any derived type
181Assert.Catch&lt;ApplicationException&gt;( code );
182
183// Allow any kind of exception
184Assert.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&nbsp;Started</a></li>
200<li><a href="assertions.html">Assertions</a></li>
201<ul>
202<li><a href="equalityAsserts.html">Equality&nbsp;Asserts</a></li>
203<li><a href="identityAsserts.html">Identity&nbsp;Asserts</a></li>
204<li><a href="conditionAsserts.html">Condition&nbsp;Asserts</a></li>
205<li><a href="comparisonAsserts.html">Comparison&nbsp;Asserts</a></li>
206<li><a href="typeAsserts.html">Type&nbsp;Asserts</a></li>
207<li id="current"><a href="exceptionAsserts.html">Exception&nbsp;Asserts</a></li>
208<li><a href="utilityAsserts.html">Utility&nbsp;Methods</a></li>
209<li><a href="stringAssert.html">String&nbsp;Assert</a></li>
210<li><a href="collectionAssert.html">Collection&nbsp;Assert</a></li>
211<li><a href="fileAssert.html">File&nbsp;Assert</a></li>
212<li><a href="directoryAssert.html">Directory&nbsp;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&nbsp;Tests</a></li>
217<li><a href="extensibility.html">Extensibility</a></li>
218<li><a href="releaseNotes.html">Release&nbsp;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 &copy; 2010 Charlie Poole. All Rights Reserved.
230</div>
231<!-- End of Footer -->
232
233</body>
234</html>
Note: See TracBrowser for help on using the repository browser.