`ShouldBeOfType/ShouldBe` should test for type equality instead of "type or derived type"
See original GitHub issueAt the moment ShouldBeOfType
is implemented this way:
public static void ShouldBeOfType(this object actual, Type expected)
{
if (actual == null)
throw new SpecificationException ...
if (!expected.IsAssignableFrom(actual.GetType()))
throw new SpecificationException ...
}
Which is especially bad for specs like …
It should_not_allow_the_transfer =
() => exception.ShouldBeOfType<Exception>();
… because this spec would succeed for a thrown IOException
too.
I think it would be better to use type equality:
if (expected != actual.GetType())
throw new SpecificationException ...
This would be of course a breaking change, which I think would be OK, because a) we are pre-1.0 and b) this would be the expected behavior of ShouldBeOfType
.
We could add an additional ShouldBeAssignableTo
(or ShouldBeOfTypeOrDerivedFrom
) to provide the old behavior too (and a simple upgrade path).
Furthermore this would solve the inconsistency with ShouldNotBeOfType
, which does already an equality-compare.
Issue Analytics
- State:
- Created 10 years ago
- Comments:14 (12 by maintainers)
Top Results From Across the Web
Equality Test for Derived Classes in C++ [duplicate]
Can different derived classes make equal objects? If so: double dispatch is an option: it ... This avoids type-checks in all derived classes....
Read more >Type.IsSubclassOf(Type) Method (System)
Determines whether the current Type derives from the specified Type. ... This method also returns false if c and the current Type are...
Read more >Use typeid to test type equality : class hierarchy - C++ Tutorial
Use typeid to test type equality : class hierarchy « Class « C++ Tutorial. b and bb are of the same type. d...
Read more >Access to a static member of a type via a derived type
This error arises in code that accesses a static member of a type via a type that was derived from it. For example:...
Read more >A function from type equality to Leibniz
The Scala standard library provides evidence of two types being equal at the data level: a value of type (A =:= B) witnesses...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
FWIW, I’m for the breaking change because I think:
ShouldBeOfType
performs an exact match than that it performs an assignability match (possibly because I know other frameworks),BTW, an option to make the breaking change even more explicit would be to mark
ShouldBeOfType
[Obsolete]
(with an explanatory message) and instead addShouldBeOfExactType
andShouldBeAssignableTo
.@danielmarbach Yes, of course this would be a breaking change, and I tried to argue in the issue description why we should still go this way. The comparison with NUnit/xUnit is just one more argument.
Regarding “how we handle that gracefully”: We could provide a new method with the “type or derived type” behavior (see issue description) and document this in the release notes.