简体   繁体   English

C#Fluent断言ShouldBeEquivalentTo的全局选项

[英]C# Fluent Assertions global options for ShouldBeEquivalentTo

In Fluent Assertions when comparing objects with DateTime properties there are sometimes a slight mismatch in the milliseconds and the comparison fail. 在Fluent Assertions中,将对象与DateTime属性进行比较时,有时会在毫秒内出现轻微的不匹配,并且比较失败。 The way we get around it is to set the comparison option like so: 我们解决它的方法是设置比较选项,如下所示:

actual.ShouldBeEquivalentTo(expected,
        options =>
            options.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation))
                .WhenTypeIs<DateTime>());

Is there a way to set this up once and have it always apply instead of having to specify it every time we call ShouldBeEquivalentTo? 有没有办法设置一次并让它始终适用,而不是每次调用ShouldBeEquivalentTo时都必须指定它?

Update1: Tried the following approach but it doesn't seem to work, test fails on 1 millisecond difference. Update1:​​尝试了以下方法,但它似乎不起作用,测试失败1毫秒的差异。 The new default does not seem to get called by the factory. 工厂似乎没有调用新的默认值。

using System;
using FluentAssertions;
using FluentAssertions.Equivalency;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    class Test
    {
        public DateTime TestDateTime { get; set; }
    }

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void SettingFluentAssertionDefault()
        {
            // arrange
            var defaultAssertionOptions = EquivalencyAssertionOptions<DateTime>.Default;

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

            var testDateTime = DateTime.Now;
            var expected = new Test {TestDateTime = testDateTime};

            // act
            var actual = new Test {TestDateTime = testDateTime.AddMilliseconds(1)};

            // assert
            actual.ShouldBeEquivalentTo(expected);
        }
    }
}

Now this can be done with the AssertionOptions static class. 现在可以使用AssertionOptions静态类完成此操作。 To use a simple example: 使用一个简单的例子:

[TestInitialize]
public void TestInit() {
  AssertionOptions.AssertEquivalencyUsing(options => options.ExcludingMissingMembers());
}

Or as in the example above: 或者如上例所示:

AssertionOptions.AssertEquivalencyUsing(options =>
  options.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation)).WhenTypeIs<DateTime>()
);

Actually, you can. 实际上,你可以。 The default configuration factory is exposed by the static property EquivalencyAssertionOptions<Test>.Default . 默认配置工厂由static属性EquivalencyAssertionOptions<Test>.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<Test>.Default;

EquivalencyAssertionOptions<Test>.Default = () =>
{
    var config = defaultAssertionOptions();
    config.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation)).WhenTypeIs<DateTime>();
    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. 如果你想要你可以得到当前的默认值并将其收集在你从工厂方法中使用的某个变量中。

I am afraid the closest thing you can come to, is providing new methods 我担心你最接近的是提供新的方法

public static void ShouldBeEquivalentToDef<T>(this T subject, object expectation, string reason = "",
    params object[] reasonArgs)
{
    ShouldBeEquivalentToDef(subject, expectation, config => config, reason, reasonArgs);
}

public static void ShouldBeEquivalentToDef<T>(this T subject, object expectation,
    Func<EquivalencyAssertionOptions<T>, EquivalencyAssertionOptions<T>> 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<T>.Default());
    defConstructedOptions.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation))
            .WhenTypeIs<DateTime>()

    new EquivalencyValidator(defConstructedOptions).AssertEquality(context);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM