使用FluentAssertions 6,您似乎可以在对象图中更长地验证Enum是否等同于字符串。来源:https://fluentassertions.com/upgradingtov6
enum MyEnum {
A,
B
}
class Source {
MyEnum Enum { get;set;}
}
class Expectation {
string Enum { get;set;}
}
var source = new Source() { Enum = MyEnum.A };
var expectation = new Expectation() {Enum = "A"};
//With V6 this assertion will fail but in V5 it will pass
expectation.Should().BeEquivalentTo(source, options => options.ComparingEnumsByName());如何使用FluentAssertions断言上面的对象?我想要的行为是在枚举的ToString表示上进行断言。
正如我顺便指出的,当我将expectation与source交换时,我会得到不同的行为。它们不应该是等价物吗?
发布于 2022-02-15 22:42:34
您可以定义一个更轻松的等效步长来处理字符串/枚举比较。
class RelaxedEnumEquivalencyStep : IEquivalencyStep
{
public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
{
if (comparands.Subject is string subject && comparands.Expectation?.GetType().IsEnum == true)
{
AssertionScope.Current
.ForCondition(subject == comparands.Expectation.ToString())
.FailWith(() =>
{
decimal? subjectsUnderlyingValue = ExtractDecimal(comparands.Subject);
decimal? expectationsUnderlyingValue = ExtractDecimal(comparands.Expectation);
string subjectsName = GetDisplayNameForEnumComparison(comparands.Subject, subjectsUnderlyingValue);
string expectationName = GetDisplayNameForEnumComparison(comparands.Expectation, expectationsUnderlyingValue);
return new FailReason(
$"Expected {{context:string}} to be equivalent to {expectationName}{{reason}}, but found {subjectsName}.");
});
return EquivalencyResult.AssertionCompleted;
}
if (comparands.Subject?.GetType().IsEnum == true && comparands.Expectation is string expectation)
{
AssertionScope.Current
.ForCondition(comparands.Subject.ToString() == expectation)
.FailWith(() =>
{
decimal? subjectsUnderlyingValue = ExtractDecimal(comparands.Subject);
decimal? expectationsUnderlyingValue = ExtractDecimal(comparands.Expectation);
string subjectsName = GetDisplayNameForEnumComparison(comparands.Subject, subjectsUnderlyingValue);
string expectationName = GetDisplayNameForEnumComparison(comparands.Expectation, expectationsUnderlyingValue);
return new FailReason(
$"Expected {{context:enum}} to be equivalent to {expectationName}{{reason}}, but found {subjectsName}.");
});
return EquivalencyResult.AssertionCompleted;
}
return EquivalencyResult.ContinueWithNext;
}
private static string GetDisplayNameForEnumComparison(object o, decimal? v)
{
if (o is null)
{
return "<null>";
}
if (v is null)
{
return '\"' + o.ToString() + '\"';
}
string typePart = o.GetType().Name;
string namePart = o.ToString().Replace(", ", "|", StringComparison.Ordinal);
string valuePart = v.Value.ToString(CultureInfo.InvariantCulture);
return $"{typePart}.{namePart} {{{{value: {valuePart}}}}}";
}
private static decimal? ExtractDecimal(object o)
{
return o?.GetType().IsEnum == true ? Convert.ToDecimal(o, CultureInfo.InvariantCulture) : null;
}
}如果只是为了一次测试
var source = new Source() { Enum = MyEnum.A };
var expectation = new Expectation() { Enum = "A" };
expectation.Should().BeEquivalentTo(source, options => options.Using(new RelaxedEnumEquivalencyStep()));
source.Should().BeEquivalentTo(expectation, options => options.Using(new RelaxedEnumEquivalencyStep()));或者,如果需要全局设置,可以使用AssertionOptions.AssertEquivalencyUsing进行设置。
AssemblyInitialize中。AssertionOptions.AssertEquivalencyUsing(e => e.Using(new RelaxedEnumEquivalencyStep()));
var source = new Source() { Enum = MyEnum.A };
var expectation = new Expectation() { Enum = "A" };
expectation.Should().BeEquivalentTo(source);
source.Should().BeEquivalentTo(expectation);为了完整起见,这里是MyEnum和string不匹配时失败消息的示例。
Expected property root.Enum to be equivalent to "B", but found MyEnum.A {value: 0}.
Expected property source.Enum to be <null>, but found MyEnum.B {value: 1}.
Expected property root.Enum to be equivalent to MyEnum.A {value: 0}, but found "B".
Expected property expectation.Enum to be equivalent to MyEnum.B {value: 1}, but found <null>.发布于 2022-02-15 18:56:27
您的期望将Enum属性定义为字符串,您提供的选项用于指导FA如何将期望的成员与主题进行比较。因此,在这种情况下,ComparingEnumsByName什么都不做,因为所涉及的属性是一个string。相反,您可以使用匿名类型作为期望。
https://stackoverflow.com/questions/71131277
复制相似问题