C# Fluent Assertions global options for ShouldBeEquivalentTo

前端 未结 3 1806
我在风中等你
我在风中等你 2021-02-19 03:41

In Fluent Assertions when comparing objects with DateTime properties there are sometimes a slight mismatch in the milliseconds and the comparison fail. The way we get around it

3条回答
  •  醉酒成梦
    2021-02-19 04:14

    Actually, you can. The default configuration factory is exposed by the static property EquivalencyAssertionOptions.Default. You can easily assign an alternative configuration for a particular data type, or extend the default configuration with additional behavior. Something like:

    var defaultAssertionOptions = EquivalencyAssertionOptions.Default;
    
    EquivalencyAssertionOptions.Default = () =>
    {
        var config = defaultAssertionOptions();
        config.Using(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation)).WhenTypeIs();
        return config;
    };
    

    If you want you can get the current default and tuck that away in some variable that you use from your factory method.

提交回复
热议问题