c# - 在 xUnit 中跳过集合相等性中的类型检查
问题描述
有一个微不足道的继承
private class TestBase : IEquatable<TestBase> {
public string Name { get; set; } = "test1";
public bool Equals(TestBase other) => this.Name == other.Name;
}
private class Test : TestBase {}
我需要比较两个集合:
var s1 = new TestBase();
var s2 = new Test();
Assert.Equal(s1, s2); // OK, uses IEquatable
Assert.StrictEqual(s1, s2); // OK uses IEquatable
Assert.Equal(new[] { s1 }, new[] { s2 }); // fails
Assert.Equal<IEnumerable<TestBase>>(new[] { s1 }, new[] { s2 }); // fails
Assert.Equal(new TestBase[] { s1 }, new TestBase[] { s2 }); // fails
对我来说,如果Assert.Equal()
单个实例使用IEquatable
接口,则集合重载也应该使用接口并且不比较类型。我如何获得想要的行为?
解决方案
您是否尝试过使用 Fluent Assertions 进行此比较?
我想到了类似的东西
x = new[] {s1};
y = new[] {s2};
x.Should().BeEquivalentTo(y, o => o.Excluding(p => p.Type));
它比较了两个对象集合彼此等价但忽略了类型。我还没有尝试使用类型的 Excluding() 部分,但它适用于单个对象属性,所以也许尝试让它也适用于您的示例。
有关 Fluent 断言的更多信息:https ://fluentassertions.com/about/
推荐阅读
- c# - 如何在 Aspnet.core Razor 的 foreach 循环中使用模型动态填充 HTML?
- python - 如何通过索引访问嵌套字典值
- curl - Facebook Graph API:无法使用 curl 获取用户性别
- google-sheets - Zoho creator 在尝试将 creator-form-data 集成到谷歌表格时收到无效的 JSON 有效负载
- powershell - 获取 PSObject 数组大小或计数
- python - pipenv --docker 的系统选项。在 docker 中获取所有 python 包的建议方法是什么
- conceptnet - 与调优相关的概念网
- canvas - 如何在three.js中制作纹理网格对象灰度?
- c# - 如何对中间显示消息框的 Windows 窗体方法进行单元测试
- android - Firebase 侦听器跳过