- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个复杂的对象,里面有不同的类型(对象、字符串、 double 等)。我想使用以下代码比较它们:
myActualObject.ShouldBeEquivalentTo(myExpectedObject, options => options
.Using<double>(dbl => dbl.Subject.Should().BeApproximately(dbl.Expectation,
ABS_ERROR))
.WhenTypeIs<double>()
);
我正在尝试使用 .BeApproximately
行为来比较 double 类型的属性值,但由于一种特殊情况而无法正常工作:
有时我在 double 中有一个 double.NaN
,将一个 NaN 与另一个 NaN 进行比较会导致断言失败(因为 BeApproximately 行为)。在这种情况下,我如何才能跳过比较,或使其为真?
最后,如何打印导致断言失败的对象的名称?
提前致谢。
编辑>>>>
我找到了第一种方法(感谢@Sam Holder):
myActualObject.ShouldBeEquivalentTo(myExpectedObject, options => options
.Using<double>(ctx => CompareDoubleApprox(ctx, myExpectedObject.Name))
.WhenTypeIs<double>()
);
...
public void CompareDoubleApprox(IAssertionContext<double> ctx, string Scope)
{
bool IsDoubleOrInfinite = (double.IsNaN(ctx.Expectation) || double.IsInfinity(ctx.Expectation));
bool absoluteErrorOK = Math.Abs(ctx.Subject - ctx.Expectation) < ABS_ERROR;
Execute.Assertion
.BecauseOf(ctx.Reason, ctx.ReasonArgs)
.ForCondition(IsDoubleOrInfinite || absoluteErrorOK)
.FailWith("{3}: Expected {context:double} to be {0} +/- {1} {reason}, but found {2}", ctx.Subject, ABS_ERROR, ctx.Expectation, Scope);
}
我的断言转储看起来像:
"MyObjectName": Expected member Foo.Bar to be 0 +/- 1E-05 , but found 1,39675
"MyObjectName": Expected member Foo.FooBar to be 2,07781 +/- 1E-05 , but found 2,98412
...More assertion fails...
我希望它在打印所有失败之前仅打印一次 objectName。
更新>>>>>
我想要的行为在github上还没有实现:https://github.com/dennisdoomen/fluentassertions/issues/310 .被标记为增强。感谢您的帮助。
最佳答案
未经测试的暗杀,但可能像这样工作:
myActualObject.ShouldBeEquivalentTo(myExpectedObject, options => options
.Using<double>(dbl =>
{
if (!double.IsNaN(dbl.Subject))
{
String errorMessage = string.Format("{0} caused the assertion to fail",myExpectedObject);
dbl.Subject.Should().BeApproximately(dbl.Expectation, ABS_ERROR, errorMessage);
}
})
.WhenTypeIs<double>()
);
关于c# - FluentAssertions WhenType 是 double.NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33671908/
如果我下载 FluentAssertions 1.7.1(我的所有单元测试当前都是针对它编写的),那么我可以引用该 dll,并且下面的测试将正常工作。 但我尝试通过 NuGet 并使用 FluentA
我一直在使用 FluentAssertions 进行单元测试,并开始研究断言是否正确抛出异常。我知道我可以使用 ExpectedExceptions 方法属性,但如果可能的话,我想学习 FluentA
这段代码工作正常 [Test] public void boo() { var collection = new[] { 1, 2, 3 };
我有这样的单元测试代码: // version3 should be a copy of version2 with some differences version3.Data.ShouldBeEq
FluentAssertions 中检查两个对象不相等的最佳方法是什么,如果类没有实现 Equals 而我想用反射进行逐字段比较?基本上我想要 Should().BeEquivalentTo() 的对
我有一个嵌套的 class和 FluentAssertions可以断言它们。 然后我改变class至 struct并且测试失败。 (如果我将 IEnumerable MyItems { get; se
我尝试使用 FluentAssertions 检查我的单元测试,项目列表中的属性类型属于特定类型。 myObj.Items.OfType().Single() .MyPrope
有没有办法使用 FluentAssertions 做这样的事情 response.Satisfy(r => r.Property1== "something" && r.Propert
我正在将 FluentAssertions 与 NUnit 一起使用,但我意识到 Throw() 方法和其他相关方法并未列出供我使用。我是否必须安装任何其他软件包才能访问此方法? 我使用的是 NuGe
因为我有一些角度,所以我想检查角度模数 360°: double angle = 0; double expectedAngle = 360; angle.Should().B
我想知道如何使用 Fluent Assertions 检查一个对象是否从另一个类继承? 我知道我可以使用 IsAssignableFrom 使用 xUnit 来做到这一点,如下所示: [Fact] p
因为我有一些角度,所以我想检查角度模数 360°: double angle = 0; double expectedAngle = 360; angle.Should().B
从我们的数据库中,我们查询放在 ExpandoObject 类型的动态对象集合中的记录集。实现 IDictionary在田野上。这些是实际值。 从我们的 SpecFlow BDD 测试中,我们得到了一
可能是一个简单的,但无法让它工作; 我已将方法上的签名一更改为任务 在我的单元测试中,我使用了流畅的断言。 但无法让它工作: _catalogVehicleMapper
我有以下规范 BidirectionalGraph Fixture = new BidirectionalGraph(); public void VerticesShouldBeAbleToAsso
我尝试不仅将 FluentAssertions 用作测试断言框架,而且还用于检查运行时契约(如高级 Debug.Assert,我已阅读 this 问题)。期望的行为是: 在 Debug模式下:抛出异常
如何比较具有名称相同但类型不同的属性的对象? public class A { public Guid Id { get; set; } } public class B { publ
我正在尝试使用最新版本的 FluentAssertions (4.0.1) 更新我的 Unittest 项目,但由于 API 的更改,我的测试不再编译。在更新之前,我使用的是 3.4.1 版,以下代码
我有一个包含各种类型的字段/属性等的类: public string SomeStringData; public int? SomeNullableIntegerData; public SomeD
当我遇到一个我预计会失败但实际通过的测试时,我正在为一个实用程序库编写一些单元测试。该问题涉及比较两个 float 变量,而不是比较一个 float? 和一个 float 变量。 我使用的是 NUni
我是一名优秀的程序员,十分优秀!