我正在使用 NUnit 2.5.3 TestCaseSource 属性并创建一个工厂来生成我的测试。像这样的东西:
[Test, TestCaseSource(typeof(TestCaseFactories), "VariableString")]
public void Does_Pass_Standard_Description_Tests(string text)
{
Item obj = new Item();
obj.Description = text;
}
我的来源是这样的:
public static IEnumerable<TestCaseData> VariableString
{
get
{
yield return new TestCaseData(string.Empty).Throws(typeof(PreconditionException))
.SetName("Does_Reject_Empty_Text");
yield return new TestCaseData(null).Throws(typeof(PreconditionException))
.SetName("Does_Reject_Null_Text");
yield return new TestCaseData(" ").Throws(typeof(PreconditionException))
.SetName("Does_Reject_Whitespace_Text");
}
}
我需要做的是向变量字符串添加最大长度检查,但是这个最大长度是在被测类的合同中定义的。在我们的例子中,它是一个简单的公共结构:
public struct ItemLengths
{
public const int Description = 255;
}
我找不到任何将值传递给测试用例生成器的方法。我已经尝试过静态共享值,但这些没有被拾取。我不想将东西保存到文件中,因为每次代码更改时我都需要重新生成这个文件。
我想将以下行添加到我的测试用例中:
yield return new TestCaseData(new string('A', MAX_LENGTH_HERE + 1))
.Throws(typeof(PreconditionException));
概念上相当简单的东西,但我发现不可能做的事情。有什么建议么?