[英]Too many arguments provided using params keyword with NUNIT in C#
您好,我正在使用 NUNIT 框架为 WPF 应用程序编写一些单元测试,但我正面临这个问题:
我写我的正面测试用例是这样的:
public static IEnumerable<TestCaseData> ThrowExceptionOnAnyStringNull_ShouldNotThrowException_TestCases
{
get
{
yield return new TestCaseData(new string[] { "", "first test argument name" });
yield return new TestCaseData(new string[] { "test string", "first test argument name" });
yield return new TestCaseData(new string[] { "first", "first name", "second", "second name", "third", "third name" });
}
}
我是这样使用它的:
[TestCaseSource(nameof(ThrowExceptionOnAnyStringNull_ShouldNotThrowException_TestCases))]
public void ThrowExceptionOnAnyStringNull_ShouldNotThrowException (params string[] args)
{
Assert.DoesNotThrow(() => ArgumentChecker.ThrowExceptionOnAnyStringNull(args));
}
要测试的方法的签名是:
public static void ThrowExceptionOnAnyStringNull (params string[] argsAndNames)
我在运行测试时从 Visual Studio 收到的错误是:提供的参数太多
我尝试添加第一个参数,就像我在其他测试中所做的一样! 但该参数仍未使用。
这是与该额外参数一起使用的代码:
public static IEnumerable<TestCaseData> ThrowExceptionOnAnyStringNull_ShouldNotThrowException_TestCases
{
get
{
yield return new TestCaseData(null, new string[] { "", "first test argument name" });
yield return new TestCaseData(null, new string[] { "test string", "first test argument name" });
yield return new TestCaseData(null, new string[] { "first", "first name", "second", "second name", "third", "third name" });
}
}
[TestCaseSource(nameof(ThrowExceptionOnAnyStringNull_ShouldNotThrowException_TestCases))]
public void ThrowExceptionOnAnyStringNull_ShouldNotThrowException (Type expectedExceptionType, params string[] args)
{
Assert.DoesNotThrow(() => ArgumentChecker.ThrowExceptionOnAnyStringNull(args));
}
我还有其他 53 个测试,这种结构的 TestCaseSource 目前工作正常。
我怎样才能摆脱那个额外的参数以获得干净的代码?
谢谢你。
额外信息:
这就是我的负面测试用例的样子:
public static IEnumerable<TestCaseData> ThrowExceptionOnAnyStringNull_ShouldThrowException_TestCases
{
get
{
// Empty list
yield return new TestCaseData(typeof(ArgumentNullException), null);
// List with odd number of elements
yield return new TestCaseData(typeof(ArgumentException) , new string[] { null });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { "" });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { "test string" });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { null, "first test argument name", null });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { "", "first test argument name", "" });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { "test string", "first test argument name", "other" });
// List with even number of elements
yield return new TestCaseData(typeof(ArgumentNullException), new string[] { null, null });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { null, "" });
yield return new TestCaseData(typeof(ArgumentNullException), new string[] { null, "first test argument name" });
yield return new TestCaseData(typeof(ArgumentNullException), new string[] { "", null });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { "", "" });
yield return new TestCaseData(typeof(ArgumentNullException), new string[] { "test string", null });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { "test string", "" });
yield return new TestCaseData(typeof(ArgumentNullException), new string[] { "first", "first name", null, "second name", "third", "third name" });
}
}
[TestCaseSource(nameof(ThrowExceptionOnAnyStringNull_ShouldThrowException_TestCases))]
public void ThrowExceptionOnAnyStringNull_ShouldThrowException (Type expectedExceptionType, params string[] args)
{
Assert.Throws(expectedExceptionType, () => ArgumentChecker.ThrowExceptionOnAnyStringNull(args));
}
尝试:
yield return new TestCaseData(new object[] { new string[] { <string array elements> }})
问题是object[]
可从string[]
分配,因此您的string[]
被解析为每个元素都是一个单独的参数,而不是整个元素是一个单独的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.