A feature request asks that Assert.Throws be modified so that
it succeeds if an exception derived from the type specified
is thrown. The submitter says that this is in keeping with the
Liskov substitution principle.
My own thought in implementing it was to provide a more localized
substitute for ExpectedException, which has always required the
exact type. The newer syntax allows the user to either require
an exact type or allow derived types by specifying
Assert.That( delegate{...}, Throws.Typeof(...) OR
Assert.That( delegate(...), Throws.InstanceOf(...)
so the question here is what is the best behavior for the
simpler Assert.Throws.
Thoughts please?
Charlie
From: nun...@googlegroups.com [mailto:nun...@googlegroups.com] On Behalf Of Simone Busoli
Sent: Sunday, September 28, 2008 2:41 PM
To: nun...@googlegroups.com
Subject: [nunitv3] Re: Assert.Throws: Should it be exact type?
Sent: Sunday, September 28, 2008 11:59 PM
I would go back to the principle of least surprise on this one. For
me, I have always asserted that it would be a particular type, but
hadn't considered what would happen if a subclass of that exception
were thrown. Now that I consider it though, I think that I would be
surprised if a test failed because it was a subclass of the base
exception. In other words, I think that the simpler Assert.Throws
should succeed even if a subclass of the original tested for Exception
type were thrown.
If they want the more specific behavior, they can use the TypeOf
syntax, so nothing is really lost. I can't imagine a scenario where
you would be sad if a subclass of the exception were thrown instead of
the base, but perhaps I have a limited imagination, as I don't create
subclasses of Exception terribly often.
-Kelly
From: nun...@googlegroups.com [mailto:nun...@googlegroups.com] On Behalf Of Jim Newkirk
Sent: Tuesday, September 30, 2008 10:37 AM
I'd really be surprised myself if you were writing a test and
didn't know what kind of exception was being thrown. :-)
Wouldn't it violate the same principle if you convert from use
of ExpectedException to Assert.Throws and find you are
getting different results?
Charlie
> -----Original Message-----
> From: nun...@googlegroups.com
> [mailto:nun...@googlegroups.com] On Behalf Of Kelly Anderson
> Sent: Tuesday, September 30, 2008 10:03 AM
> To: nun...@googlegroups.com
> Subject: [nunitv3] Re: Assert.Throws: Should it be exact type?
>
>
I don't have very strong feelings on this subject, and can see both
sides of the argument. Clearly not changing behavior from one version
to the next is also very important.
-Kelly
From: nun...@googlegroups.com [mailto:nun...@googlegroups.com] On Behalf Of Jim Newkirk
Sent: Tuesday, September 30, 2008 10:37 AM