question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`ShouldBeOfType/ShouldBe` should test for type equality instead of "type or derived type"

See original GitHub issue

At 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:closed
  • Created 10 years ago
  • Comments:14 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
fschmiedcommented, Jan 15, 2014

FWIW, I’m for the breaking change because I think:

  • it’s less surprising to me that ShouldBeOfType performs an exact match than that it performs an assignability match (possibly because I know other frameworks),
  • the breaking change is explicit (i.e., it creates red tests after upgrading rather than silently doing the wrong thing), and
  • code relying on the old behavior is easily fixed (you can search/replace if you want the old behavior).

BTW, an option to make the breaking change even more explicit would be to mark ShouldBeOfType [Obsolete] (with an explanatory message) and instead add ShouldBeOfExactType and ShouldBeAssignableTo.

1reaction
ulrichbcommented, Nov 8, 2013

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found