英文:
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<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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论