c# - 流利的断言:如何断言“集合中的单个等效项”?
问题描述
在 MSTest 单元测试中,我需要断言给定集合恰好包含与给定项等效的单个项,但流利的断言似乎只支持这些方法:
items.Should().ContainEquivalentOf(item);
items.Should().ContainSingle(item);
我需要两者的结合,比如“ContainSingleEquivalentOf(item)”(似乎不存在?)。
问题是 ContainSingle() 似乎只使用我需要的对象不支持的普通 Equals() 方法检查等价性,所以我需要使用流利断言提供的基于反射的等价性检查。但我不知道如何在这种情况下使用它。
编辑:该集合允许包含与我的标准不匹配的任意其他项目。
解决方案
没有内置的方法可以断言一个集合只包含一个等价物。但是由于 Fluent Assertions 的可扩展性,我们可以构建它。
这是一个原型ContainSingleEquivalentOf
。虽然它有一些限制。例如,Where(e => !ReferenceEquals(e, match.Which))
断言它只会排除一个项目。
public static class Extensions
{
public static AndWhichConstraint<GenericCollectionAssertions<T>, T> ContainSingleEquivalentOf<T, TExpectation>(this GenericCollectionAssertions<T> parent,
TExpectation expected, string because = "", params string[] becauseArgs) => parent.ContainSingleEquivalentOf(expected, config => config, because, becauseArgs);
public static AndWhichConstraint<GenericCollectionAssertions<T>, T> ContainSingleEquivalentOf<T, TExpectation>(this GenericCollectionAssertions<T> parent,
TExpectation expected, Func<EquivalencyAssertionOptions<TExpectation>, EquivalencyAssertionOptions<TExpectation>> config, string because = "", params string[] becauseArgs)
{
var match = parent.ContainEquivalentOf(expected, config, because, becauseArgs);
var remainingItems = parent.Subject.Where(e => !ReferenceEquals(e, match.Which)).ToList();
remainingItems.Should().NotContainEquivalentOf(expected, config, because, becauseArgs);
return match;
}
}
请注意,Fluent Assertions 将Equals
在被期望覆盖以比较实例时使用。要覆盖该行为,您可以使用ComparingByMembers
或提供匿名对象作为预期。
class MyClass
{
public int MyProperty { get; set; }
public override bool Equals(object obj) => false;
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void Force_comparing_by_members()
{
var subject = new MyClass[] { new() { MyProperty = 42 } };
var expected = new MyClass { MyProperty = 42 };
subject.Should().ContainSingleEquivalentOf(expected, opt => opt.ComparingByMembers<MyClass>());
}
[TestMethod]
public void Use_anonymous_expectation_to_compare_by_members()
{
var subject = new MyClass[] { new() { MyProperty = 42 } };
var expected = new { MyProperty = 42 };
subject.Should().ContainSingleEquivalentOf(expected);
}
[TestMethod]
public void Multiple_equivalent_items()
{
var subject = new MyClass[] { new() { MyProperty = 42 }, new() { MyProperty = 42 } };
var expected = new { MyProperty = 42 };
Action act = () => subject.Should().ContainSingleEquivalentOf(expected);
act.Should().Throw<Exception>();
}
[TestMethod]
public void No_equivalent_item()
{
var subject = new MyClass[] { new() { MyProperty = -1 } };
var expected = new { MyProperty = 42 };
Action act = () => subject.Should().ContainSingleEquivalentOf(expected);
act.Should().Throw<Exception>();
}
[TestMethod]
public void Fails_as_Fluent_Assertions_uses_overriden_Equals_method()
{
var subject = new MyClass[] { new() { MyProperty = 42 } };
var expected = new MyClass { MyProperty = 42 };
Action act = () => subject.Should().ContainSingleEquivalentOf(expected);
act.Should().Throw<Exception>();
}
}