我有一个像这样的对象字典:Attributes = new Dictionary<string, object>();
在字典中是DateTime
,如果DateTime
落在某一天,我正在尝试测试,如下所示:
Attributes[key].ShouldBeEquivalentTo(DateTime.Now, options => options
.Using<DateTime>(ctx =>
{
//throw new InvalidOperationException();
ctx.Subject.Should().BeSameDateAs(ctx.Expectation);
}).WhenTypeIs<DateTime>());
然而,这会导致断言失败Expected subject to be <2016-06-30 11:38:05.447>, but found <2016-06-30 10:38:05>
。日期是在同一天,所以我认为应该通过,但断言失败了。
这使我得出结论,ctx.Subject.Should().BeSameDateAs(ctx.Expectation)
行未被应用。我尝试添加一个异常,并进行调试,但似乎动作中的代码永远不会到达。
我应该期待这项工作吗?我这样做了吗?
答案 0 :(得分:2)
永远不会达到代码,因为ShouldBeEquivalentTo
应该比较对象图(实际和预期的属性)。 Using...WhenTypeIs<DateTime>
将应用于DateTime
类型的属性。
在你的情况下,对象本身是DateTime
,所以你可以断言它确实是DateTime
并链接其他所需的断言:
Attributes[key].Should().BeOfType<DateTime>().Which.Should().BeSameDateAs(DateTime.Now);