-
Assertion using FluentAssertions;
namespace TestProject;
public abstract class Strategy { }
public class NoOperationStrategy : Strategy { }
public class NumberStrategy : Strategy
{
public int Number { get; }
public NumberStrategy(int number)
{
Number = number;
}
}
public class UnitTest
{
[Fact]
public void Test()
{
SomeMethod().Should().BeEquivalentTo(
new Strategy[] { new NoOperationStrategy(), new NumberStrategy(1), new NumberStrategy(2) },
cfg => cfg.IncludingAllRuntimeProperties());
}
private IEnumerable<Strategy> SomeMethod() => new Strategy[]
{
new NoOperationStrategy(),
new NumberStrategy(1),
new NumberStrategy(2)
};
} I would expect empty non-null object to be always equivalent to empty non-null object. |
Beta Was this translation helpful? Give feedback.
Answered by
jnyrup
Feb 24, 2024
Replies: 1 comment 1 reply
-
Here's a workaround for the described case. class NoOperationStrategyStep : IEquivalencyStep
{
public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator nestedValidator)
{
if (comparands.Expectation is NoOperationStrategy && comparands.Subject is NoOperationStrategy)
{
return EquivalencyResult.AssertionCompleted;
}
return EquivalencyResult.ContinueWithNext;
}
} SomeMethod().Should().BeEquivalentTo(
new Strategy[] { new NoOperationStrategy(), new NumberStrategy(1), new NumberStrategy(2) },
opt => opt.IncludingAllRuntimeProperties().Using(new NoOperationStrategyStep())); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
FLAMESpl
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a workaround for the described case.
To add
IEquivalencyStep
s globally in a thread-safe manner, see https://fluentassertions.com/extensibility/#thread-safety