在顶层断言中排除EnumerableEquivalencyStep。

huangapple go评论59阅读模式
英文:

Exclude EnumerableEquivalencyStep on top level Assertion

问题

我有一个来自外部库的对象,它实现了 `IEnumerable<T>`。我只想检查这个对象的属性,而不是集合的等价性。我对 `IEnumerable` 中的内容不感兴趣。如何从断言中排除这部分?

这是一个示例类,实现了 `IEnumerable<string>`:
public class Example : IEnumerable<string>
{
    public string Id { get; set; }
    public string Name { get; set; }
    public IEnumerator<string> GetEnumerator() => new List<string> { Id }.GetEnumerator();
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

这是测试代码,它将在 `IEnumerable` 上失败:
(new Example {Id = "123", Name= "Test" } as object)
    .Should().BeEquivalentTo(new Example{Name = "Test"},
        o => o.Excluding(p => p.Id));

在执行测试之前,你可以这样做,但是这会失去检查类上任何 `IEnumerable` 属性的能力:
AssertionOptions.EquivalencyPlan.Remove<GenericEnumerableEquivalencyStep>();
AssertionOptions.EquivalencyPlan.Remove<EnumerableEquivalencyStep>();
英文:

I have this object from an external library that implements IEnumerable<T>. I just want to check the properties of this object and not the equivalence of the collection. I'm not interested in the stuff inside the IEnumerable. How can I exclude this from the assertion?

public class Example : IEnumerable<string>
{
    public string Id { get; set; }
    public string Name { get; set; }
    public IEnumerator<string> GetEnumerator() => new List<string> { Id }.GetEnumerator();
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

This is the test, which will fail on the IEnumerable.

(new Example {Id = "123", Name= "Test" } as object)
    .Should().BeEquivalentTo(new Example{Name = "Test"},
        o => o.Excluding(p => p.Id));

Doing this before the test this will work, but then I lose the ability to check any IEnumerable property on the class:

AssertionOptions.EquivalencyPlan.Remove<GenericEnumerableEquivalencyStep>();
AssertionOptions.EquivalencyPlan.Remove<EnumerableEquivalencyStep>();

答案1

得分: 1

以下是您提供的内容的中文翻译:

解决此限制的一种方法是添加一个专用的等效性步骤来处理Example

using FluentAssertions;
using FluentAssertions.Equivalency;
using FluentAssertions.Equivalency.Steps;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

[TestClass]
public class UnitTest1
{
    [ModuleInitializer]
    public static void SetDefaults()
    {
        AssertionOptions.EquivalencyPlan.Insert<ExampleEquivalencyStep>();
    }

    [TestMethod]
    public void TestMethod1()
    {
        object subject = new Example { Id = "123", Name = "Test" };
        var expected = new Example { Name = "Test" };
        subject.Should().BeEquivalentTo(expected, o => o.Excluding(p => p.Id));
    }
}

class ExampleEquivalencyStep : EquivalencyStep<Example>
{
    protected override EquivalencyResult OnHandle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator) =>
        new StructuralEqualityEquivalencyStep().Handle(comparands, context, nestedValidator);
}

public class Example : IEnumerable<string>
{
    public string Id { get; set; }
    public string Name { get; set; }
    public IEnumerator<string> GetEnumerator() => new List<string> { Id }.GetEnumerator();
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

希望这有帮助。如果您有任何其他需要,请随时提问。

英文:

One way to workaround this limitation is to add a dedicated equivalency step to handle Example.

using FluentAssertions;
using FluentAssertions.Equivalency;
using FluentAssertions.Equivalency.Steps;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

[TestClass]
public class UnitTest1
{
    [ModuleInitializer]
    public static void SetDefaults()
    {
        AssertionOptions.EquivalencyPlan.Insert&lt;ExampleEquivalencyStep&gt;();
    }

    [TestMethod]
    public void TestMethod1()
    {
        object subject = new Example { Id = &quot;123&quot;, Name = &quot;Test&quot; };
        var expected = new Example { Name = &quot;Test&quot; };
        subject.Should().BeEquivalentTo(expected, o =&gt; o.Excluding(p =&gt; p.Id));
    }
}

class ExampleEquivalencyStep : EquivalencyStep&lt;Example&gt;
{
    protected override EquivalencyResult OnHandle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator) =&gt;
        new StructuralEqualityEquivalencyStep().Handle(comparands, context, nestedValidator);
}

public class Example : IEnumerable&lt;string&gt;
{
    public string Id { get; set; }
    public string Name { get; set; }
    public IEnumerator&lt;string&gt; GetEnumerator() =&gt; new List&lt;string&gt; { Id }.GetEnumerator();
    IEnumerator IEnumerable.GetEnumerator() =&gt; GetEnumerator();
}

huangapple
  • 本文由 发表于 2023年6月13日 17:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定