Fluent Assertions 中的 Satisfy() 不适用于 class 个对象的集合
Satisfy() in Fluent Assertions does not work with collections of class objects
我有一个 class:
public class TestClass
{
public int Id { get; set; }
public int CampusId { get; set; }
public int CurrentStudentCount { get; set; }
public int MaxStudentCount { get; set; }
}
和这个 class 的对象集合:
var collection = new[]
{
new TestClass
{
Id = 55,
CampusId = 38,
CurrentStudentCount = 1,
MaxStudentCount = 2
},
new TestClass
{
Id = 127,
CampusId = 38,
CurrentStudentCount = 2,
MaxStudentCount = 2
},
new TestClass
{
Id = 126,
CampusId = 38,
CurrentStudentCount = 2,
MaxStudentCount = 2
}
};
我想断言每个对象的 CampusId
等于 38:
collection.Should().Satisfy(i => i.CampusId == 38);
但是断言失败并显示以下消息:
预期集合满足所有谓词,但以下元素不匹配任何谓词:
Index: 1, Element: TestClass
{
CampusId = 38,
CurrentStudentCount = 2,
Id = 127,
MaxStudentCount = 2
}
Index: 2, Element: TestClass
{
CampusId = 38,
CurrentStudentCount = 2,
Id = 126,
MaxStudentCount = 2
}
Satisfy
(和 SatisfyRespectively
)需要集合中每个元素的 lambda。在你的情况下是:
collection.Should().Satisfy(
i => i.CampusId == 38,
i => i.CampusId == 38,
i => i.CampusId == 38
);
另一种选择是使用 OnlyContain
:
collection.Should().OnlyContain(i => i.CampusId == 38);
我有一个 class:
public class TestClass
{
public int Id { get; set; }
public int CampusId { get; set; }
public int CurrentStudentCount { get; set; }
public int MaxStudentCount { get; set; }
}
和这个 class 的对象集合:
var collection = new[]
{
new TestClass
{
Id = 55,
CampusId = 38,
CurrentStudentCount = 1,
MaxStudentCount = 2
},
new TestClass
{
Id = 127,
CampusId = 38,
CurrentStudentCount = 2,
MaxStudentCount = 2
},
new TestClass
{
Id = 126,
CampusId = 38,
CurrentStudentCount = 2,
MaxStudentCount = 2
}
};
我想断言每个对象的 CampusId
等于 38:
collection.Should().Satisfy(i => i.CampusId == 38);
但是断言失败并显示以下消息:
预期集合满足所有谓词,但以下元素不匹配任何谓词:
Index: 1, Element: TestClass
{
CampusId = 38,
CurrentStudentCount = 2,
Id = 127,
MaxStudentCount = 2
}
Index: 2, Element: TestClass
{
CampusId = 38,
CurrentStudentCount = 2,
Id = 126,
MaxStudentCount = 2
}
Satisfy
(和 SatisfyRespectively
)需要集合中每个元素的 lambda。在你的情况下是:
collection.Should().Satisfy(
i => i.CampusId == 38,
i => i.CampusId == 38,
i => i.CampusId == 38
);
另一种选择是使用 OnlyContain
:
collection.Should().OnlyContain(i => i.CampusId == 38);