Fluent Assertions:检查集合是否包含对象,相当于提供的对象?
Fluent Assertions: check if the collection contains the object, equivalent to the provided one?
我正在使用 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" });
}
没有优雅的方式,但你可以使用谓词:
[Test]
public void ShouldContainStudent()
{
new[] { new Student { Name = "George" }, new Student { Name = "Anna" } }
.Should().Contain(s => s.Name == "Anna");
}
我正在使用 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" });
}
没有优雅的方式,但你可以使用谓词:
[Test]
public void ShouldContainStudent()
{
new[] { new Student { Name = "George" }, new Student { Name = "Anna" } }
.Should().Contain(s => s.Name == "Anna");
}