C# Fluent Assertions global options for ShouldBeEquivalentTo

前端 未结 3 1796
我在风中等你
我在风中等你 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:04

    I am afraid the closest thing you can come to, is providing new methods

    public static void ShouldBeEquivalentToDef(this T subject, object expectation, string reason = "",
        params object[] reasonArgs)
    {
        ShouldBeEquivalentToDef(subject, expectation, config => config, reason, reasonArgs);
    }
    
    public static void ShouldBeEquivalentToDef(this T subject, object expectation,
        Func, EquivalencyAssertionOptions> config, string reason = "", params object[] reasonArgs)
    {
        var context = new EquivalencyValidationContext
        {
            Subject = subject,
            Expectation = expectation,
            CompileTimeType = typeof (T),
            Reason = reason,
            ReasonArgs = reasonArgs
        };
    
        var defConstructedOptions = config(EquivalencyAssertionOptions.Default());
        defConstructedOptions.Using(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation))
                .WhenTypeIs()
    
        new EquivalencyValidator(defConstructedOptions).AssertEquality(context);
    }
    

提交回复
热议问题