-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Investigate why Option doesn't play well with FluentAssertions .BeEquivalentTo() #96
Comments
This concerned me for a bit because of implications for That being said, I'll keep tabs on this in case a |
I've had some issues in the past, but I never actually determined the source of the problem. It's possible this is a non-issue, but I wanted to do some testing just to make sure. |
I believe I finally encountered this issue. It popped up when using [Fact]
// this test currently fails
public void BeEquivalentTo()
{
var parentA = new ParentA(new ChildA("TEST"));
var parentB = new ParentB(new ChildB("TEST"));
parentA.Should().BeEquivalentTo(parentB);
}
private class ParentA
{
public ParentA(Option<ChildA> child)
{
Child = child;
}
public Option<ChildA> Child { get; }
}
private class ParentB
{
public ParentB(Option<ChildB> child)
{
Child = child;
}
public Option<ChildB> Child { get; }
}
private abstract class BaseChild
{
protected BaseChild(string value)
{
Value = value;
}
public string Value { get; }
}
private class ChildA : BaseChild
{
public ChildA(string value)
: base(value)
{
}
}
private class ChildB : BaseChild
{
public ChildB(string value)
: base(value)
{
}
}
|
Fixed it by defining the following equivalency step: private class OptionEquivalencyStep<T> : IEquivalencyStep
{
public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
{
if (!(comparands.Subject is Option<T> subjectOption))
return EquivalencyResult.ContinueWithNext;
if (!(comparands.Expectation is Option<T> expectationOption))
return EquivalencyResult.ContinueWithNext;
subjectOption.HasValue().Should().Be(expectationOption.HasValue());
if (!subjectOption.HasValue() || !expectationOption.HasValue())
return EquivalencyResult.AssertionCompleted;
var subject = subjectOption.ThrowOnNone(() => new InvalidOperationException("Expected value in subject!"));
var expectation = expectationOption.ThrowOnNone(() => new InvalidOperationException("Expected value in expectation!"));
subject.Should().BeEquivalentTo(expectation);
return EquivalencyResult.AssertionCompleted;
}
}
|
Any idea why it doesn't just compare the internal fields as I expected? I'm hoping there is an alteration we can make to the primitives to make it "just work". |
That I don't know |
I also encountered the issue when comparing collections of private class SimpleClass
{
public SimpleClass(Int32 value)
{
Value = value;
}
public Int32 Value { get; }
}
[Fact]
public void SampleTest()
{
var optionList1 = new[] { Option.Some(new SimpleClass(1)), Option.None(), Option.Some(new SimpleClass(2)) };
var optionList2 = new[] { Option.Some(new SimpleClass(1)), Option.None(), Option.Some(new SimpleClass(2)) };
// this fails
optionList1.Should().BeEquivalentTo(optionList2);
}
// this works
optionList1.Should().BeEquivalentTo(optionList2, options => options.Using(new OptionEquivalencyStep<SimpleClass>())); Modifying I'm guessing that the problem is how this line and
The type |
No description provided.
The text was updated successfully, but these errors were encountered: