C ++ Fluent断言的ShouldBeEquivalentTo全局选项 [英] C# Fluent Assertions global options for ShouldBeEquivalentTo

查看:102
本文介绍了C ++ Fluent断言的ShouldBeEquivalentTo全局选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Fluent断言中,当比较具有DateTime属性的对象时,有时会在毫秒内略有不匹配,并且比较会失败.解决这个问题的方法是像这样设置比较选项:

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 is to set the comparison option like so:

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

是否有一种方法可以设置一次并使其始终应用,而不是每次我们调用ShouldBeEquivalentTo时都必须指定它?

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?

Update1: 尝试了以下方法,但似乎不起作用,测试在1毫秒的差异上失败.新的默认值似乎没有被工厂调用.

Update1: Tried the following approach but it doesn't seem to work, test fails on 1 millisecond difference. 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);
        }
    }
}

推荐答案

实际上,您可以.默认配置工厂由static属性EquivalencyAssertionOptions<Test>.Default公开.您可以轻松地为特定数据类型分配替代配置,或通过其他行为扩展默认配置.像这样:

Actually, you can. The default configuration factory is exposed by the static property 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.

这篇关于C ++ Fluent断言的ShouldBeEquivalentTo全局选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆