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 - ConstraintModel</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>Constraint-Based Assert Model (NUnit 2.4)</h2>
|
---|
28 |
|
---|
29 | <p>The constraint-based Assert model uses a single method of the Assert class
|
---|
30 | for all assertions. The logic necessary to carry out each assertion is
|
---|
31 | embedded in the constraint object passed as the second parameter to that
|
---|
32 | method.
|
---|
33 |
|
---|
34 | <p>Here's a very simple assert using the constraint model:
|
---|
35 | <pre> Assert.That( myString, Is.EqualTo("Hello") );</pre>
|
---|
36 |
|
---|
37 | <p>The second argument in this assertion uses one of NUnit's <b>syntax helpers</b>
|
---|
38 | to create an <b>EqualConstraint</b>. The same assertion could also be made in this form:
|
---|
39 | <pre> Assert.That( myString, new EqualConstraint("Hello") );</pre>
|
---|
40 |
|
---|
41 | <p>Using this model, all assertions are made using one of the forms of the
|
---|
42 | Assert.That() method, which has a number of overloads...
|
---|
43 |
|
---|
44 | <div class="code" style="width: 36em">
|
---|
45 | <pre>
|
---|
46 | Assert.That( object actual, IResolveConstraint constraint )
|
---|
47 | Assert.That( object actual, IResolveConstraint constraint,
|
---|
48 | string message )
|
---|
49 | Assert.That( object actual, IResolveConstraint constraint,
|
---|
50 | string message, object[] parms )
|
---|
51 |
|
---|
52 | Assert.That( ActualValueDelegate del, IResolveConstraint constraint )
|
---|
53 | Assert.That( ActualValueDelegate del, IResolveConstraint constraint,
|
---|
54 | string message )
|
---|
55 | Assert.That( ActualValueDelegate del, IResolveConstraint constraint,
|
---|
56 | string message, object[] parms )
|
---|
57 |
|
---|
58 | Assert.That<T>( ref T actual, IResolveConstraint constraint )
|
---|
59 | Assert.That<T>( ref T actual, IResolveConstraint constraint,
|
---|
60 | string message )
|
---|
61 | Assert.That<T>( ref T actual, IResolveConstraint constraint,
|
---|
62 | string message, object[] parms )
|
---|
63 |
|
---|
64 | Assert.That( bool condition );
|
---|
65 | Assert.That( bool condition, string message );
|
---|
66 | Assert.That( bool condition, string message, object[] parms );
|
---|
67 |
|
---|
68 | Assert.That( TestDelegate del, IResolveConstraint constraint );
|
---|
69 | </pre>
|
---|
70 | </div>
|
---|
71 |
|
---|
72 | <p>If you derive your test fixture class from <b>AssertionHelper</b>, the
|
---|
73 | Expect() method may be used in place of Assert.That()...
|
---|
74 |
|
---|
75 | <div class="code" style="width: 36em">
|
---|
76 | <pre>
|
---|
77 | Expect( object actual, IResolveConstraint constraint )
|
---|
78 | Expect( object actual, IResolveConstraint constraint,
|
---|
79 | string message )
|
---|
80 | Expect( object actual, IResolveConstraint constraint,
|
---|
81 | string message, object[] parms )
|
---|
82 |
|
---|
83 | Expect( ActualValueDelegate del, IResolveConstraint constraint )
|
---|
84 | Expect( ActualValueDelegate del, IResolveConstraint constraint,
|
---|
85 | string message )
|
---|
86 | Expect( ActualValueDelegate del, IResolveConstraint constraint,
|
---|
87 | string message, object[] parms )
|
---|
88 |
|
---|
89 | Expect<T>( ref T actual, IResolveConstraint constraint )
|
---|
90 | Expect<T>( ref T actual, IResolveConstraint constraint,
|
---|
91 | string message )
|
---|
92 | Expect<T>( ref T actual, IResolveConstraint constraint,
|
---|
93 | string message, object[] parms )
|
---|
94 |
|
---|
95 | Expect( bool condition );
|
---|
96 | Expect( bool condition, string message );
|
---|
97 | Expect( bool condition, string message, object[] parms );
|
---|
98 |
|
---|
99 | Expect( TestDelegate del, IResolveConstraint constraint );
|
---|
100 | </pre>
|
---|
101 | </div>
|
---|
102 |
|
---|
103 | <p>The overloads that take a bool work exactly like Assert.IsTrue.
|
---|
104 |
|
---|
105 | <p>For overloads taking a constraint, the argument must be a object implementing
|
---|
106 | the <b>IConstraint</b> interface, which supports performing a test
|
---|
107 | on an actual value and generating appropriate messages. This interface
|
---|
108 | is described in more detail under
|
---|
109 | <a href="customConstraints.html">Custom Constraints</a>.
|
---|
110 |
|
---|
111 | <p>NUnit provides a number of constraint classes similar to the <b>EqualConstraint</b>
|
---|
112 | used in the example above. Generally, these classes may be used directly or
|
---|
113 | through a syntax helper. Test fixture classes inheriting from <b>AssertionHelper</b>
|
---|
114 | are able to use shorter forms. The valid forms are described on the pages related to
|
---|
115 | each constraint. Note that the menu items listed to the right generally reflect the
|
---|
116 | names of the syntax helpers.
|
---|
117 |
|
---|
118 | <p><b>See also:</b> the
|
---|
119 | <a href="classicModel.html">classic model</a> of assertions.
|
---|
120 |
|
---|
121 |
|
---|
122 | </div>
|
---|
123 |
|
---|
124 | <!-- Submenu -->
|
---|
125 | <div id="subnav">
|
---|
126 | <ul>
|
---|
127 | <li><a href="index.html">NUnit 2.5.10</a></li>
|
---|
128 | <ul>
|
---|
129 | <li><a href="getStarted.html">Getting Started</a></li>
|
---|
130 | <li><a href="assertions.html">Assertions</a></li>
|
---|
131 | <li id="current"><a href="constraintModel.html">Constraints</a></li>
|
---|
132 | <ul>
|
---|
133 | <li><a href="equalConstraint.html">Equal Constraint</a></li>
|
---|
134 | <li><a href="sameasConstraint.html">SameAs Constraint</a></li>
|
---|
135 | <li><a href="conditionConstraints.html">Condition Constraints</a></li>
|
---|
136 | <li><a href="comparisonConstraints.html">Comparison Constrants</a></li>
|
---|
137 | <li><a href="pathConstraints.html">Path Constraints</a></li>
|
---|
138 | <li><a href="typeConstraints.html">Type Constraints</a></li>
|
---|
139 | <li><a href="stringConstraints.html">String Constraints</a></li>
|
---|
140 | <li><a href="collectionConstraints.html">Collection Constraints</a></li>
|
---|
141 | <li><a href="propertyConstraint.html">Property Constraint</a></li>
|
---|
142 | <li><a href="throwsConstraint.html">Throws Constraint</a></li>
|
---|
143 | <li><a href="compoundConstraints.html">Compound Constraints</a></li>
|
---|
144 | <li><a href="delayedConstraint.html">Delayed Constraint</a></li>
|
---|
145 | <li><a href="listMapper.html">List Mapper</a></li>
|
---|
146 | <li><a href="reusableConstraint.html">Reusable Constraint</a></li>
|
---|
147 | </ul>
|
---|
148 | <li><a href="attributes.html">Attributes</a></li>
|
---|
149 | <li><a href="runningTests.html">Running Tests</a></li>
|
---|
150 | <li><a href="extensibility.html">Extensibility</a></li>
|
---|
151 | <li><a href="releaseNotes.html">Release Notes</a></li>
|
---|
152 | <li><a href="samples.html">Samples</a></li>
|
---|
153 | <li><a href="license.html">License</a></li>
|
---|
154 | </ul>
|
---|
155 | </ul>
|
---|
156 | </div>
|
---|
157 | <!-- End of Submenu -->
|
---|
158 |
|
---|
159 |
|
---|
160 | <!-- Standard Footer for NUnit.org -->
|
---|
161 | <div id="footer">
|
---|
162 | Copyright © 2010 Charlie Poole. All Rights Reserved.
|
---|
163 | </div>
|
---|
164 | <!-- End of Footer -->
|
---|
165 |
|
---|
166 | </body>
|
---|
167 | </html>
|
---|