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,
|
---|
30 | throws a particular exception. It may be used alone, to merely test the type
|
---|
31 | of constraint, or with an additional constraint to be applied to the exception
|
---|
32 | specified as an argument.
|
---|
33 |
|
---|
34 | p>The related <b>ThrowsNothingConstraint</b> simply asserts that the delegate
|
---|
35 | does not throw an exception.
|
---|
36 |
|
---|
37 | <h4>Constructors</h4>
|
---|
38 | <div class="code"><pre>
|
---|
39 | ThrowsConstraint(Type expectedType)
|
---|
40 | ThrowsConstraint<T>()
|
---|
41 | ThrowsConstraint(Type expectedType, Constraint constraint)
|
---|
42 | ThrowsConstraint<T>(Constraint constraint)
|
---|
43 | ThrowsNothingConstraint()
|
---|
44 | </pre></div>
|
---|
45 |
|
---|
46 | <h4>Syntax</h4>
|
---|
47 | <div class="code"><pre>
|
---|
48 | Throws.Exception
|
---|
49 | Throws.TargetInvocationException
|
---|
50 | Throws.ArgumentException
|
---|
51 | Throws.InvalidOperationException
|
---|
52 | Throws.TypeOf(Type expectedType)
|
---|
53 | Throws.TypeOf<T>()
|
---|
54 | Throws.InstanceOf(Type expectedType)
|
---|
55 | Throws.InstanceOf<T>()
|
---|
56 | Throws.Nothing
|
---|
57 | Throws.InnerException
|
---|
58 | </pre></div>
|
---|
59 |
|
---|
60 | <h4>Examples of Use</h4>
|
---|
61 | <div class="code"><pre>
|
---|
62 | // .NET 1.1
|
---|
63 | Assert.That( new TestDelegate(SomeMethod),
|
---|
64 | Throws.TypeOf(typeof(ArgumentException)));
|
---|
65 | Assert.That( new TestDelegate(SomeMethod),
|
---|
66 | Throws.Exception.TypeOf(typeof(ArgumentException)));
|
---|
67 | Assert.That( new TestDelegate(SomeMethod),
|
---|
68 | Throws.TypeOf(typeof(ArgumentException))
|
---|
69 | .With.Property("Parameter").EqualTo("myParam"));
|
---|
70 | Assert.That( new TestDelegate(SomeMethod),
|
---|
71 | Throws.ArgumentException );
|
---|
72 | Assert.That( new TestDelegate(SomeMethod),
|
---|
73 | Throws.TargetInvocationException
|
---|
74 | .With.InnerException.TypeOf(ArgumentException));
|
---|
75 |
|
---|
76 | // .NET 2.0
|
---|
77 | Assert.That( SomeMethod,
|
---|
78 | Throws.TypeOf<ArgumentException>());
|
---|
79 | Assert.That( SomeMethod,
|
---|
80 | Throws.Exception.TypeOf<ArgumentException>());
|
---|
81 | Assert.That( SomeMethod,
|
---|
82 | Throws.TypeOf<ArgumentException>()
|
---|
83 | .With.Property("Parameter").EqualTo("myParam"));
|
---|
84 | Assert.That( SomeMethod, Throws.ArgumentException );
|
---|
85 | Assert.That( SomeMethod,
|
---|
86 | Throws.TargetInvocationException
|
---|
87 | .With.InnerException.TypeOf<ArgumentException>());
|
---|
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 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 Constraint</a></li>
|
---|
124 | <li><a href="sameasConstraint.html">SameAs Constraint</a></li>
|
---|
125 | <li><a href="conditionConstraints.html">Condition Constraints</a></li>
|
---|
126 | <li><a href="comparisonConstraints.html">Comparison Constrants</a></li>
|
---|
127 | <li><a href="pathConstraints.html">Path Constraints</a></li>
|
---|
128 | <li><a href="typeConstraints.html">Type Constraints</a></li>
|
---|
129 | <li><a href="stringConstraints.html">String Constraints</a></li>
|
---|
130 | <li><a href="collectionConstraints.html">Collection Constraints</a></li>
|
---|
131 | <li><a href="propertyConstraint.html">Property Constraint</a></li>
|
---|
132 | <li id="current"><a href="throwsConstraint.html">Throws Constraint</a></li>
|
---|
133 | <li><a href="compoundConstraints.html">Compound Constraints</a></li>
|
---|
134 | <li><a href="delayedConstraint.html">Delayed Constraint</a></li>
|
---|
135 | <li><a href="listMapper.html">List Mapper</a></li>
|
---|
136 | <li><a href="reusableConstraint.html">Reusable Constraint</a></li>
|
---|
137 | </ul>
|
---|
138 | <li><a href="attributes.html">Attributes</a></li>
|
---|
139 | <li><a href="runningTests.html">Running Tests</a></li>
|
---|
140 | <li><a href="extensibility.html">Extensibility</a></li>
|
---|
141 | <li><a href="releaseNotes.html">Release 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 © 2010 Charlie Poole. All Rights Reserved.
|
---|
153 | </div>
|
---|
154 | <!-- End of Footer -->
|
---|
155 |
|
---|
156 | </body>
|
---|
157 | </html>
|
---|