C#学习教程:FluentAssertion:粗略比较类属性使用FluentAssertions以给定的精度大致比较所有属性或选择的属性的最佳方法是什么?目前我一直在这样做:calculated.X.Should().BeApproximately(expected.X,precision);calculated.Y.Should().BeApproximately(expected.Y,precision);calculated.Z.Should().BeApproximately(expected.Z,precision);是否有一条直线可以实现相同的目标?就像使用ShouldBeEquivalentTo一样,还是我需要构建一个允许包含/排除属性的通用扩展方法?是的,可以使用ShouldBeEquivalentTo。以下代码将检查所有精度为0.1的双精度属性:doubleprecision=0.1;calculated.ShouldBeEquivalentTo(expected,option=>options.Using(ctx=>ctx.Subject.Should().BeApproximately(ctx.Expectation,precision)).WhenTypeIs());如果只想比较X、Y和Z属性,请像这样更改When约束:doubleprecision=0.1;calculated.ShouldBeEquivalentTo(b,options=>options.Using(ctx=>ctx.Subject.Should().BeApproximately(ctx.Expectation,precision)).When(info=>info.SelectedMemberPath=="X"||信息.SelectedMemberPath=="Y"||info.SelectedMemberPath=="Z"));另一种方法是显式告诉FluentAssertions应该比较属性,但不太优雅:doubleprecision=0.1;calculated.ShouldBeEquivalentTo(b,options=>options.Including(info=>info.SelectedMemberPath=="X"||info.SelectedMemberPath=="Y"||info.SelectedMemberPath=="Z").Using(ctx=>ctx.Subject.Should().BeApproximately(ctx.Expectation,precision)).When(info=>true));由于Using语句不返回EquivalencyAssertionOptions,我们需要通过使用始终为真的表达式调用When语句来破解它。以上就是C#学习教程:fluentassertion:粗略比较类属性共享的所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
