Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#流畅的断言全球选项应与_C#_Fluent Assertions - Fatal编程技术网

C#流畅的断言全球选项应与

C#流畅的断言全球选项应与,c#,fluent-assertions,C#,Fluent Assertions,在Fluent断言中,当比较对象与DateTime属性时,有时毫秒内会出现轻微的不匹配,并且比较失败。我们绕过它的方法是如下设置比较选项: actual.ShouldBeEquivalentTo(expected, options => options.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation)) .Wh

在Fluent断言中,当比较对象与DateTime属性时,有时毫秒内会出现轻微的不匹配,并且比较失败。我们绕过它的方法是如下设置比较选项:

actual.ShouldBeEquivalentTo(expected,
        options =>
            options.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation))
                .WhenTypeIs<DateTime>());
实际值应与(预期值)相等,
选项=>
options.Using(ctx=>ctx.Subject.Should().BeCloseTo(ctx.Expectation))
.WhenTypeIs());
有没有一种方法可以设置一次并始终应用它,而不是每次调用shouldbeeequivalento时都指定它

更新1: 尝试了以下方法,但似乎不起作用,测试在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 = new Test {TestDateTime = testDateTime.AddMilliseconds(1)};

            // assert
            actual.ShouldBeEquivalentTo(expected);
        }
    }
}
使用系统;
使用FluentAssertions;
使用FluentAssertions。等效性;
使用Microsoft.VisualStudio.TestTools.UnitTesting;
命名空间UnitTestProject1
{
课堂测试
{
public DateTime TestDateTime{get;set;}
}
[测试类]
公共类UnitTest1
{
[测试方法]
公共无效设置FluentAssertionDefault()
{
//安排
var defaultAssertionOptions=equalityassertionoptions.Default;
EquivalencyAssertionOptions.Default=()=>
{
var config=defaultAssertionOptions();
config.Using(ctx=>ctx.Subject.Should().BeCloseTo(ctx.Expectation)).WhenTypeIs();
返回配置;
};
var testDateTime=DateTime.Now;
var预期=新测试{TestDateTime=TestDateTime};
//表演
var-actual=newtest{TestDateTime=TestDateTime.addmillizes(1)};
//断言
实际。应等同于(预期);
}
}
}

恐怕最接近的方法就是提供新的方法

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);
}
public static void应该是等价的todef(此T主题、对象期望、字符串原因=”,
参数对象[]推理参数(args)
{
ShouldBeEquivalentToDef(主题、期望、配置=>config、reason、reasonArgs);
}
公共静态无效应等同于定义(该主题、对象期望、,
Func config,字符串reason=“”,参数对象[]reasonArgs)
{
var context=new equalencyValidationContext
{
主语,
期望=期望,
CompileTimeType=typeof(T),
理性,
ReasonArgs=ReasonArgs
};
var defConstructedOptions=config(equalityAssertionOptions.Default());
defConstructedOptions.Using(ctx=>ctx.Subject.Should().BeCloseTo(ctx.Expectation))
.WhenTypeIs()
新的等价验证器(defConstructedOptions).AssertEquality(上下文);
}

实际上,你可以。默认配置工厂由
static
属性
equalityAssertionOptions.default
公开。您可以轻松地为特定数据类型分配替代配置,或者使用其他行为扩展默认配置。比如:

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;
};
var defaultAssertionOptions=equalityassertionoptions.Default;
EquivalencyAssertionOptions.Default=()=>
{
var config=defaultAssertionOptions();
config.Using(ctx=>ctx.Subject.Should().BeCloseTo(ctx.Expectation)).WhenTypeIs();
返回配置;
};

如果需要,可以获取当前默认值,并将其塞进从factory方法使用的某个变量中。

现在可以使用AssertionOptions静态类完成此操作。要使用一个简单的示例:

[TestInitialize]
public void TestInit() {
  AssertionOptions.AssertEquivalencyUsing(options => options.ExcludingMissingMembers());
}
或如上例所示:

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

也许我做错了,但我尝试将以下内容添加到测试初始化中,但在工厂实例化时,似乎没有调用它。EquivalencyAssertionOptions.Default=()=>{var config=defaultAssertionOptions();config.Using(ctx=>ctx.Subject.Should().BeCloseTo(ctx.Expectation)).WhenTypeIs();return config;};此外,由于EquivalencyAssertionOptions的构造函数是私有的,所以建议的代码没有编译。实际上,我的示例是错误的。由于您正在对
Test
类的实例执行断言,因此必须覆盖该特定类的默认选项,而不是内部使用的
DateTime
。我刚刚用你的例子测试了这一点,并更新了我的原始答案。太棒了!谢谢。TestInitialize本身被称为吗?是的,TestInitialize被称为。我甚至在执行检查之前将代码片段直接添加到测试中,但它似乎没有被击中。用一个完整的测试单元更新问题。以防有人像我一样愚蠢——记住,这些选项是静态的,在整个测试运行过程中都会保持不变(当然这是O.P.的要求)。我做了一个测试,设置选项忽略具有特定名称的属性上的等价性比较,假设在每个测试中都重新分配了等价性比较。事实并非如此,它每次都会添加一个新规则,因此每个测试都会使用前一个测试的“忽略列表”以及自己的“忽略列表”。吸取的教训。。。。