高階約束使用
大的流暢斷言確實變得難以閱讀,但是當與具有良好實現 ToString()
的類組合時,它們可以生成非常有用的錯誤訊息。
[Test]
public void AdvancedContraintsGiveUsefulErrorMessages() {
Assert.That(actualCollection, Has
.Count.EqualTo(4)
.And.Exactly(1).Property("Age").GreaterThan(60)
.And.Some.Property("Address").Null
.And.No.Property("Age").LessThanOrEqualTo(17));
}
失敗時,此斷言會生成如下訊息:
Expected: property Count equal to 4 and exactly one item property Age greater
than 60 and some item property Address null and not property Age less than or
equal to 17
But was: < <Steve Taylor (23) lives in Newcastle
>, <Michelle Yung (65) lives in San Francisco
>, <Ranjit Saraman (49) lives in Milano
>, <LaChelle Oppenheimer (16) lives in
> >