Re: Testing for errors
Christian Fröbel <[email protected]>
| Newsgroups | gmane.comp.windows.dotnet.nunit.user |
|---|---|
| Message-ID | <[email protected]> |
Hi Charlie,
> Hi Christian,
>
>
>> [...]
>>
>
> Of course, you could use try/catch but that would expose the implementation
> to the test. As a side note, this doesn't bother most of us - the test is
> supposed to have knowledge of the external implementation of the methods
> it tests and any exception is part of that.
>
Technically yes. But this is exactly what I don't want.
Tests also serve as documentation. And if I would expose that
exception-stuff in the tests, people might think that they are free to
handle it. -- But they aren't. The only way to deal with it is to fix
the program and rebuild.
Also, it's kind of verbose to write those try-catch-blocks everywhere.
> You could also use an ExpectedException attribute, like this...
>
>
>> [Test, ExpectedException(typeof(DeveloperError-exception))]
>> void at_errorIfIndexOutOfRange()
>> {
>> // setUp Vector v with some elements
>> int outOfRange = -1;
>> v.at (outOfRange);
>> }
>>
>
>
Ok, less verbose, but still that exception-stuff.
> To wrap the processing in an assert as you outlined, the problem
> is that exception is thrown as soon as the argument is evaluated
> and the IsError method is never called.
Exactly.
> The way around this is
> to use a delegate, either explicit or anonymous depending on the
> version of .NET you are using.
>
I thought of delegates, too. But couldn't find a way to implement it
(I'm on .NET 2.0). Of course, the at()-method is not the only one I need
to wrap in the IsError()-method. Thus I need some kind of generic
delegate for that, I guess. Is that even possible in C#? I would need
variable arguments: 0-n of any type; and also variable return types:
void or any other type. -- But this is what you refer to as explicit, right?
Never heard of anonymous delegates before. Let me do some digging....
Ah, I guess I got the pointer. Do you mean something like this?:
Assert.IsError (delegate() {v.at (outOfRange);});
Hmm, not that bad. Looks kind of ruby-ish to me:)
So the IsError()-method would be declared like this, I guess:
delegate void Callback();
void IsError (Callback toBeTestedForError);
I'll try that later and report if it worked....
Thanks and regards,
Christian
> IMO, this is a bit involved for the case as described... I'd just
> test for the exception, but it's certainly possible if you really
> want to.
>
> Charlie
>
>
>
> -------------------------------------------------------------------------
> 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/
> _______________________________________________
> Nunit-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/nunit-users
>
-------------------------------------------------------------------------
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/