Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在NUnit TestCase中将单个值传递给params参数_C#_Unit Testing_Nunit_Parameter Passing - Fatal编程技术网

C# 在NUnit TestCase中将单个值传递给params参数

C# 在NUnit TestCase中将单个值传递给params参数,c#,unit-testing,nunit,parameter-passing,C#,Unit Testing,Nunit,Parameter Passing,我有以下测试: [ExpectedException(typeof(ParametersParseException))] [TestCase("param1")] [TestCase("param1", "param2")] [TestCase("param1", "param2", "param3", "optParam4", "optParam5", "some extra parameter")] public void Parse_InvalidParametersNumber_Thr

我有以下测试:

[ExpectedException(typeof(ParametersParseException))]
[TestCase("param1")]
[TestCase("param1", "param2")]
[TestCase("param1", "param2", "param3", "optParam4", "optParam5", "some extra parameter")]
public void Parse_InvalidParametersNumber_ThrowsException(params string[] args)
{
    new ParametersParser(args).Parse();
}
第一个测试用例(显然)失败,出现以下错误:

System.ArgumentException : Object of type 'System.String' 
cannot be converted to type 'System.String[]'.
我试图用以下定义替换TestCase定义:

[TestCase(new[] { param1 })]
但现在我得到了以下编译错误:

错误CS0182:属性参数必须是常量表达式、typeof表达式或属性参数类型的数组创建表达式

我现在的解决方案是将“一个参数”的情况转移到另一种测试方法

但是,有没有办法让此测试以与其他测试相同的方式运行?

一种方法可以是使用并拥有一个返回每个参数集的方法,而不是使用TestCase。

基于对问题“”的响应,编译错误源于,并且可以使用命名测试用例的语法来克服,例如:

[ExpectedException(typeof(ParametersParseException))]
[TestCase(new[] { "param1"}, TestName="SingleParam")]
[TestCase(new[] { "param1", "param2"}, TestName="TwoParams")]
[TestCase(new[] { "param1", "param2", "param3", "optParam4", "optParam5"}, "some extra parameter", TestName="SeveralParams")]
public void Parse_InvalidParametersNumber_ThrowsException(params string[] args)
{
    new ParametersParser(args).Parse();
}

虽然我不推荐这种方法

然而,这里还有一种将单个参数传递给
params
数组的方法,即在
params
参数之前使用伪对象参数

请参见下面的示例:

[ExpectedException(typeof(ParametersParseException))]
[TestCase(null, "param1")]
[TestCase(null, "param1", "param2")]
[TestCase(null, "param1", "param2", "param3", "optParam4", "optParam5", "some extra parameter")]
public void Parse_InvalidParametersNumber_ThrowsException(object _, params string[] args)
{
    new ParametersParser(args).Parse();
}

PS实现这一点的更好方法是使用属性。

您链接到一个resharper问题,而问题似乎在nunit中。。。在任何情况下,resharper似乎都解决了这个问题,使用
new[]{…}
执行此操作时,数组由字符串组成,给出了上面OP引用的错误CS0182。整数类型似乎工作正常。