【发布时间】:2014-10-21 22:11:25
【问题描述】:
使用流利的断言,我想断言给定的字符串包含两个字符串之一:
actual.Should().Contain("oneWay").Or().Should().Contain("anotherWay");
// eiter value should pass the assertion.
// for example: "you may do it oneWay." should pass, but
// "you may do it thisWay." should not pass
只有两个值都不包含时,断言才会失败。这不起作用(甚至无法编译),因为没有 Or() 运算符。
这就是我现在的做法:
bool isVariant1 = actual.Contains(@"oneWay");
bool isVariant2 = actual.Contains(@"anotherWay");
bool anyVariant = (isVariant1 || isVariant2);
anyVariant.Should().BeTrue("because blahblah. Actual content was: " + actual);
这很冗长,必须手动创建“因为”参数才能获得有意义的输出。
有没有办法以更易读的方式做到这一点?解决方案也应该适用于其他流畅的断言类型,如Be()、HaveCount() 等...
我在 .NET 3.5 上使用 FluentAssertions 版本 2.2.0.0,如果这很重要的话。
【问题讨论】:
标签: c# unit-testing combinations assert fluent-assertions