相关疑难解决方法(0)

C#Fluent断言ShouldBeEquivalentTo的全局选项

在Fluent Assertions中,将对象与DateTime属性进行比较时,有时会在毫秒内出现轻微的不匹配,并且比较失败.我们解决它的方法是设置比较选项,如下所示:

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

有没有办法设置一次并让它始终适用,而不是每次调用ShouldBeEquivalentTo时都必须指定它?

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

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 …
Run Code Online (Sandbox Code Playgroud)

c# fluent-assertions

15
推荐指数
2
解决办法
8064
查看次数

标签 统计

c# ×1

fluent-assertions ×1