【问题标题】:Fluent Assertions: check if the collection contains the object, equivalent to the provided one?Fluent Assertions:检查集合是否包含对象,相当于提供的?
【发布时间】:2017-07-20 21:22:12
【问题描述】:

我正在使用 Fluent Assertions 并愿意使用深度对象图比较来测试我的集合是否包含某些对象。我不想实现所有的平等成员。但是,我找不到对集合中某些对象的等效包含进行测试的方法。例如,这个测试失败了,我希望它通过:

class Student
{
    public string Name { get; set; }
}

[Test]
public void ShouldContainStudent()
{
    new[] { new Student { Name = "George" }, new Student { Name = "Anna" } }.Should()
        .Contain(new Student { Name = "Anna" });
}

有什么优雅的方法吗?像这样?

[Test]
public void ShouldContainStudent()
{
    new[] { new Student { Name = "George" }, new Student { Name = "Anna" } }.ShouldContainEquivalent(new Student { Name = "Anna" });
}

【问题讨论】:

  • 你今天不能这样做,但从技术上讲,让它成为可能并不那么复杂。大多数内部结构已经足够可组合了。

标签: fluent-assertions


【解决方案1】:

没有优雅的方式,但你可以使用谓词:

[Test]
public void ShouldContainStudent()
{
    new[] { new Student { Name = "George" }, new Student { Name = "Anna" } }
        .Should().Contain(s => s.Name == "Anna");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2017-06-04
    • 2022-11-09
    • 1970-01-01
    • 2017-10-23
    • 2016-01-13
    相关资源
    最近更新 更多