1

Closed

Support interface inheritance with AllProperties

description

public class Car: Vehicle, ICar
    {
        public int Wheels { get; set; }
    }

    public class Vehicle : IVehicle
    {
        public int VehicleId { get; set; }
    }

    public interface ICar : IVehicle
    {
        int Wheels { get; set; }
    }

    public interface IVehicle
    {
        int VehicleId { get; set; }
    }

    [TestMethod]
    public void Should()
    {
        var expected = (ICar)new Car {VehicleId = 99999, Wheels = 4};
        var actual = (ICar)new Car {VehicleId = 1, Wheels = 4};
        actual.ShouldHave().AllProperties().EqualTo(expected);
        // this assert passes but should fail on VehicleId
    }

    [TestMethod]
    public void Should()
    {
        var expected = (IVehicle)new Car {VehicleId = 1, Wheels = 4};
        var actual = (IVehicle)new Car {VehicleId = 1, Wheels = 99999};
        actual.ShouldHave().AllProperties().EqualTo(expected);
        // this assert passes but should fail on Wheels
    }
Closed Aug 25, 2012 at 2:19 PM by
All done with the public beta of Fluent Assertions 2.0

comments

dennisdoomen wrote Apr 15, 2012 at 6:53 PM

You are right about the first example, but the second example should not throw. IVehicle only exposes the VehicleId property, so FA will only compare that property.

brianlow wrote Apr 16, 2012 at 3:22 AM

Regarding the second example: I'd like to compare DDD entities while ignoring certain properties on all entities so I was thinking of a common method like
public void AssertAreEqual(IEntity expected, IEntity actual)
{
actual.ShouldHave().AllProperties().But(d => d.DateModified).But(d => d.DateCreated).EqualTo(expected);
}

dennisdoomen wrote Apr 16, 2012 at 7:49 AM

Yes, but up until now FA has only included the properties exposed by the specified type. So supporting your scenario would be a breaking change. However, if you use define the variables as Car rather than IVehicle, it should work as expected.

brianlow wrote Apr 17, 2012 at 5:28 AM

I understand.

wrote Apr 20, 2012 at 7:36 PM

Associated with changeset 76618.