Should().Be.OfType should include subtypes

Apr 21, 2010 at 2:47 PM

According to the Liskov Substitution principle, if B is a subclass of A then anywhere A is used then an instance of B may be substituted.

The OfType<T> extension method violates this principle. I am aware that .IsAssignableTo<T> is also available, but this does not return a strong-typed fluent interface of type T, which is the problem in the following use case:

public abstract class A
    public string Property1 { get; set; }
    private class B : A {}
    private class C : A {}

object x = //somehow get an instance of B
x.Should().Be.OfType<A> //fails, x is an instance of B not A)
x.Should().Be.OfType<B> //doesn't compile, B is private)

x.Should().Be.AssignableTo<A>.And.ValueOf.Property1.Should().Be("foo"); //doesn't compile, AssignableTo does not return strong-typed interface
Do you see my problem?



Apr 21, 2010 at 9:32 PM

Please open a new ticket in issue tracker.