Testing for errors
Christian Fröbel <[email protected]>
| Newsgroups | gmane.comp.windows.dotnet.nunit.user |
|---|---|
| Message-ID | <[email protected]> |
Hi there,
I'm new to C# and new to NUnit. So far, I'm getting along just fine. But
now I'm stuck.
The situation is as follows: Let's say I have a Vector class with an
accessor-method:
Element at (int index);
This method's contract says that the index must be in the range [0,
size()-1]. If it's not, it's an error. -- And this is what I'd like to
test. So in the test I'd like to write something like this:
[Test]
void at_errorIfIndexOutOfRange()
{
// setUp Vector v with some elements
int outOfRange = -1;
Assert.IsError (v.at (outOfRange));
}
The Assert.IsError() is something I would like to implement. But I don't
know how.
The error-case is communicated via an exception (a special
DeveloperError-exception). But this is an implementation detail and I
don't want to expose that to neither the implementor of the method nor
the user of the interface nor the implementor of the test. Just to
illustrate: the implementation could look something like this:
Element at (int index)
{
Asserter.ensure (index >= 0 && index < size(), "Index out of range.");
// ...
return e;
}
So the DeveloperError-exception-stuff is hidden behind
Asserter.ensure() and
Assert.IsError()
Any ideas how to implement such a beast?
Regards,
Christian
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/