Hi All,
I think that the way the ThrowsConstraint requires a delegate mixes the action and assertion parts of the test.
Would it not be better to find a way to separate the action from the assertion?
Regards
Ian Chamberlain
From: nunit-...@googlegroups.com [mailto:nunit-...@googlegroups.com] On Behalf Of Ian Chamberlain
Sent: Saturday, August 08, 2009 6:49 PM
To: nunit-...@googlegroups.com
Subject: [nunit-discuss] ThrowsConstraint and Arrange, Act, Assert
Hi Charlie,
I suppose what I’m really looking for is an action syntax that allows an exception to be caught when I expect one, but to cause the test to fail when I don’t.
Maybe it’s more around setting an expectation and then verifying the expectation has been made.
Expect<ArgumentNullException>.When(() => new MyClass(null));
Assert.That(Expect.Thrown.ParamName, Is.EqualTo(“argument”));
Expect<ApplicationException>.When(() => DoSomething());
Assert.That(Expect.Thrown.InnerException, Is.InstanceOf<ArgumentOutOfRangeException>());
Thinking about it this really shouldn’t be that difficult. I’ll try it out and see how it works.
Regards
Ian
<BR
No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.5.392 / Virus Database: 270.13.47/2290 - Release Date: 08/08/09 18:17:00
Sent: Sunday, August 09, 2009 5:19 AM
Hi Fabio,
I like to keep the assertion separate from the action.
All the Expect is doing is telling the test not to fail if a certain type of exception is thrown and then provide access to the actual exception.
It’s up to the assertion to then establish that the expected result was achieved.
At the moment I use a try..catch block and will have different assertions for the exception type, the exception message and for any other properties that may be set on the exception, such as ParamName for ArgumentExceptions.
Regards
Ian Chamberlain
Sent: Sunday, August 09, 2009 7:35 AM
From: nunit-...@googlegroups.com [mailto:nunit-...@googlegroups.com] On Behalf Of Fabio Maulo
Sent: Sunday, August 09, 2009 12:24 PM
To: nunit-...@googlegroups.com
From: nunit-...@googlegroups.com [mailto:nunit-...@googlegroups.com] On Behalf Of Charlie Poole
Sent: Sunday, August 09, 2009 12:33 PM
> But if it's put under Assert, I think people will
> expect it to fail if no exception is thrown or (for Catch<T>) if the
> exception is not an instance of T. What do you think?
I agree! And that's all I need, so I am happy :) Thanks!
BTW, now all three forms below are identical, right?
Assert.Throws() // just my 2 cents: semantics doesn't seem to conform
with it's generic version.
Assert.Catch()
Assert.Catch<Exception>()
Cheers,
Kenneth
I really liked the fact that the name of Throws and Catch matchs
perfectly well with their behaviors. Throws<AException> means I expect
somewhere in my code does "throw new AException()", which should be
exact type. And Catch<AExceptin> analouges to catch(AException), which
is instanceof by nature.
>> BTW, now all three forms below are identical, right?
>> Assert.Throws() // just my 2 cents: semantics doesn't seem to
>> conform with it's generic version.
>> Assert.Catch()
>> Assert.Catch<Exception>()
>
> Yes, all three do the same thing as defined. I guess #2 is completely
> superfluous, but Assert.Catch<T> can be used for other types than
> just Exception and would apply an InstanceOfType constraint rather than
> the ExactTypeConstraint used by Assert.Throws<T>.
>
Yes, Assert.Catch<T> and Assert.Throws<T> are essential as they do
different things. Ideally, Assert.Catch(type, action) would be nice
for 1.x support. Assert.Catch() is superfluous but a nice shortcut for
Assert.Catch<Exception>(action) or Assert.Catch(typeof(Exception),
action).
That being said, I do hold a different view about Assert.Throws(). I
think not only it is superfluous but also it doesn't conform to the
semantics of other Assert.Throws memebers, which *always expect exact
type* of exception.
Cheers,
Kenneth