source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/lib/NUnit/NUnit-2.5.10.11092/doc/throwsConstraint.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: 5.7 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 - ThrowsConstraint</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>Throws Constraint (NUnit 2.5)</h2>
28
29<p><b>ThrowsConstraint</b> is used to test that some code, represented as a delegate,
30throws a particular exception. It may be used alone, to merely test the type
31of constraint, or with an additional constraint to be applied to the exception
32specified as an argument.
33
34p>The related <b>ThrowsNothingConstraint</b> simply asserts that the delegate
35does not throw an exception.
36
37<h4>Constructors</h4>
38<div class="code"><pre>
39ThrowsConstraint(Type expectedType)
40ThrowsConstraint&lt;T&gt;()
41ThrowsConstraint(Type expectedType, Constraint constraint)
42ThrowsConstraint&lt;T&gt;(Constraint constraint)
43ThrowsNothingConstraint()
44</pre></div>
45
46<h4>Syntax</h4>
47<div class="code"><pre>
48Throws.Exception
49Throws.TargetInvocationException
50Throws.ArgumentException
51Throws.InvalidOperationException
52Throws.TypeOf(Type expectedType)
53Throws.TypeOf&lt;T&gt;()
54Throws.InstanceOf(Type expectedType)
55Throws.InstanceOf&lt;T&gt;()
56Throws.Nothing
57Throws.InnerException
58</pre></div>
59
60<h4>Examples of Use</h4>
61<div class="code"><pre>
62// .NET 1.1
63Assert.That( new TestDelegate(SomeMethod),
64 Throws.TypeOf(typeof(ArgumentException)));
65Assert.That( new TestDelegate(SomeMethod),
66 Throws.Exception.TypeOf(typeof(ArgumentException)));
67Assert.That( new TestDelegate(SomeMethod),
68 Throws.TypeOf(typeof(ArgumentException))
69 .With.Property("Parameter").EqualTo("myParam"));
70Assert.That( new TestDelegate(SomeMethod),
71 Throws.ArgumentException );
72Assert.That( new TestDelegate(SomeMethod),
73 Throws.TargetInvocationException
74 .With.InnerException.TypeOf(ArgumentException));
75
76// .NET 2.0
77Assert.That( SomeMethod,
78 Throws.TypeOf&lt;ArgumentException&gt;());
79Assert.That( SomeMethod,
80 Throws.Exception.TypeOf&lt;ArgumentException&gt;());
81Assert.That( SomeMethod,
82 Throws.TypeOf&lt;ArgumentException&gt;()
83 .With.Property("Parameter").EqualTo("myParam"));
84Assert.That( SomeMethod, Throws.ArgumentException );
85Assert.That( SomeMethod,
86 Throws.TargetInvocationException
87 .With.InnerException.TypeOf&lt;ArgumentException&gt;());
88</pre></div>
89
90<h4>Notes</h4>
91<ol>
92<li><b>Throws.Exception</b> may be followed by further constraints,
93 which are applied to the exception itself as shown in the last two
94 examples above. It may also be used alone to verify that some
95 exception has been thrown, without regard to type. This is
96 not a recommended practice since you should normally know
97 what exception you are expecting.
98<li><b>Throws.TypeOf</b> and <b>Throws.InstanceOf</b> are provided
99 as a shorter syntax for this common test. They work exactly like
100 the corresponding forms following <b>Throws.Exception</b>.
101<li><b>Throws.TargetInvocationException/b>, <b>Throws.ArgumentException</b>
102 and <b>Throws.InvalidOperationException</b> provide a shortened form
103 for some common exceptions.
104<li>Used alone, <b>Throws.InnerException</b> simply tests the InnerException
105 value of the thrown exception. More commonly, it will be used in
106 combination with a test for the type of the outer exception as shown
107 in the examples above.
108</ol>
109
110
111
112</div>
113
114<!-- Submenu -->
115<div id="subnav">
116<ul>
117<li><a href="index.html">NUnit 2.5.10</a></li>
118<ul>
119<li><a href="getStarted.html">Getting&nbsp;Started</a></li>
120<li><a href="assertions.html">Assertions</a></li>
121<li><a href="constraintModel.html">Constraints</a></li>
122<ul>
123<li><a href="equalConstraint.html">Equal&nbsp;Constraint</a></li>
124<li><a href="sameasConstraint.html">SameAs&nbsp;Constraint</a></li>
125<li><a href="conditionConstraints.html">Condition&nbsp;Constraints</a></li>
126<li><a href="comparisonConstraints.html">Comparison&nbsp;Constrants</a></li>
127<li><a href="pathConstraints.html">Path&nbsp;Constraints</a></li>
128<li><a href="typeConstraints.html">Type&nbsp;Constraints</a></li>
129<li><a href="stringConstraints.html">String&nbsp;Constraints</a></li>
130<li><a href="collectionConstraints.html">Collection&nbsp;Constraints</a></li>
131<li><a href="propertyConstraint.html">Property&nbsp;Constraint</a></li>
132<li id="current"><a href="throwsConstraint.html">Throws&nbsp;Constraint</a></li>
133<li><a href="compoundConstraints.html">Compound&nbsp;Constraints</a></li>
134<li><a href="delayedConstraint.html">Delayed&nbsp;Constraint</a></li>
135<li><a href="listMapper.html">List&nbsp;Mapper</a></li>
136<li><a href="reusableConstraint.html">Reusable&nbsp;Constraint</a></li>
137</ul>
138<li><a href="attributes.html">Attributes</a></li>
139<li><a href="runningTests.html">Running&nbsp;Tests</a></li>
140<li><a href="extensibility.html">Extensibility</a></li>
141<li><a href="releaseNotes.html">Release&nbsp;Notes</a></li>
142<li><a href="samples.html">Samples</a></li>
143<li><a href="license.html">License</a></li>
144</ul>
145</ul>
146</div>
147<!-- End of Submenu -->
148
149
150<!-- Standard Footer for NUnit.org -->
151<div id="footer">
152 Copyright &copy; 2010 Charlie Poole. All Rights Reserved.
153</div>
154<!-- End of Footer -->
155
156</body>
157</html>
Note: See TracBrowser for help on using the repository browser.