【发布时间】:2015-08-04 14:29:08
【问题描述】:
我最近开始使用 NUnit 的约束功能并遇到以下问题。如何使用流利的表达式语法编写约束,其中执行顺序很重要,并且在普通 C# 编程中用括号解决?
在以下示例中,我定义了两个单独的断言:
- 字符串应该以 1 或 2 开头,并且在所有情况下字符串都应该以 5 结尾
- 字符串应以 1 或 2 开头,如果字符串以 2 开头,则应以 5 结尾
要断言这一点,我可以考虑三种方式;经典、流畅的约束和使用复合约束的约束。所以这会产生 6 个测试和一些测试用例。
private class SourceForParenthesisTest : IEnumerable
{
public IEnumerator GetEnumerator()
{
yield return new TestCaseData("2").Throws(typeof(AssertionException));
yield return new TestCaseData("3").Throws(typeof(AssertionException));
yield return new TestCaseData("15");
yield return new TestCaseData("25");
yield return new TestCaseData("35").Throws(typeof(AssertionException));
}
}
[TestCase("1", ExpectedException = typeof(AssertionException))]
[TestCaseSource(typeof(SourceForParenthesisTest))]
public void WithParenthesisClassic(string i)
{
var res = (i.StartsWith("1") || i.StartsWith("2")) && i.EndsWith("5");
Assert.True(res);
}
[TestCase("1", ExpectedException = typeof(AssertionException))]
[TestCaseSource(typeof(SourceForParenthesisTest))]
public void WithParenthesisOperatorConstraint(string i)
{
Assert.That(i, (Is.StringStarting("1") | Is.StringStarting("2")) & Is.StringEnding("5"));
}
[TestCase("1", ExpectedException = typeof(AssertionException), Ignore = true, IgnoreReason = "Not clear how to write this fluent expression")]
[TestCaseSource(typeof(SourceForParenthesisTest))]
public void WithParenthesisConstraint(string i)
{
Assert.That(i, Is.StringStarting("1").Or.StringStarting("2").And.StringEnding("5"));
}
[TestCase("1")]
[TestCaseSource(typeof(SourceForParenthesisTest))]
public void NoParenthesisClassic(string i)
{
var res = i.StartsWith("1") || i.StartsWith("2") && i.EndsWith("5");
Assert.True(res);
}
[TestCase("1")]
[TestCaseSource(typeof(SourceForParenthesisTest))]
public void NoParenthesisOperatorConstraint(string i)
{
Assert.That(i, Is.StringStarting("1") | Is.StringStarting("2") & Is.StringEnding("5"));
}
[TestCase("1")]
[TestCaseSource(typeof(SourceForParenthesisTest))]
public void NoParenthesisConstraint(string i)
{
Assert.That(i, Is.StringStarting("1").Or.StringStarting("2").And.StringEnding("5"));
}
实际问题出在 WithParenthesisConstraint(上面列出的断言 1)中,我想不出一种如何正确编写约束的方法,这导致我设置为忽略的一个失败的测试用例。
如何编写此断言以按预期工作?
【问题讨论】:
标签: c# unit-testing nunit fluent-interface