[英]How to correctly combine Using and WhenTypeIs<DateTime> in Fluent Assertions
I have a dictionary of objects like so: Attributes = new Dictionary<string, object>();
我有一个这样的对象字典:
Attributes = new Dictionary<string, object>();
Within the dictionary is a DateTime
, I am trying test if that DateTime
falls on a particular day, like so: 字典中有一个
DateTime
,我正在尝试测试该DateTime
是否在特定的一天,例如:
Attributes[key].ShouldBeEquivalentTo(DateTime.Now, options => options
.Using<DateTime>(ctx =>
{
//throw new InvalidOperationException();
ctx.Subject.Should().BeSameDateAs(ctx.Expectation);
}).WhenTypeIs<DateTime>());
However this results in the failed assertion Expected subject to be <2016-06-30 11:38:05.447>, but found <2016-06-30 10:38:05>
. 但是,这导致失败的断言
Expected subject to be <2016-06-30 11:38:05.447>, but found <2016-06-30 10:38:05>
。 The dates are on the same date so I believe should pass, but the assertion has failed. 日期是在同一日期,所以我相信应该通过,但是断言失败了。
This leads me to conclude that the line ctx.Subject.Should().BeSameDateAs(ctx.Expectation)
is not being applied. 这使我得出结论,没有应用
ctx.Subject.Should().BeSameDateAs(ctx.Expectation)
。 I tried adding an exception, and debugging but it appears the code in the action is never reached. 我尝试添加异常,然后进行调试,但似乎从未达到操作中的代码。
Should I expect this work? 我应该期待这项工作吗? I am doing this correctly?
我这样做正确吗?
Code in action is never reached because ShouldBeEquivalentTo
is supposed to compare object graph (properties of actual and expected). 永远不会到达运行中的代码,因为应该将
ShouldBeEquivalentTo
比较对象图(实际和预期的属性)。 Using...WhenTypeIs<DateTime>
will be applied to properties of type DateTime
. Using...WhenTypeIs<DateTime>
将应用于DateTime
类型的属性 。
In your case object is DateTime
itself, so you can assert that it is indeed DateTime
and chain other required assertions: 在您的情况下,对象是
DateTime
本身,因此您可以断言它确实是DateTime
并链接其他必需断言:
Attributes[key].Should().BeOfType<DateTime>().Which.Should().BeSameDateAs(DateTime.Now);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.