I'd like to tell FluentAssertions to check if 2 instances are of the same type, but for a nested property.
The scenario is the following:
This is the class
class Item
{
public int? Value { get; set; }
public Exception? Exception { get; set; }
}
Then, I have a test:
[Fact]
public void Test()
{
var expected = new[]
{
new Item { Value = 1 },
new Item { Exception = new TimeoutException() },
};
var actual= new[]
{
new Item { Value = 1 },
new Item { Exception = new TimeoutException() },
};
actual.Should().BeEquivalentTo(expected);
}
The test fails because the two instances of TimeoutException are different. I don't know to test for instance being equal, but being of the same type.
How can I do it?
NOTE I actually have managed to get the correct behavior doing this:
actual.Should().BeEquivalentTo(expected, options => options
.Using<Item>(context =>
{
if (context.Expectation.Exception is { Exception: { } a } && context.Subject.Exception is { Exception: { } b })
{
a.Should().BeOfType(b.GetType());
}
})
.WhenTypeIs<Item>());
But it feels wrong and suboptimal.