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