Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.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# 通过调用方法将字符串传递给属性参数_C#_.net_Nunit - Fatal编程技术网

C# 通过调用方法将字符串传递给属性参数

C# 通过调用方法将字符串传递给属性参数,c#,.net,nunit,C#,.net,Nunit,我试图使用NUnit并将字符串参数传递给TestCase属性,但得到“属性参数必须是常量表达式、typeof表达式或属性参数类型的数组创建表达式” 这是一个简化版本,但MyStatic是一个返回构建的正则表达式字符串的调用,因此MyStatic中被调用的每个方法都附加到stringbuilder,并具有到字符串的隐式转换 我想保留这种方法,因为如果我创建单独的单元测试,我将违反DRY原则 [TestCase("","/123",MyStatic.DoThis().And().GetStrin

我试图使用NUnit并将字符串参数传递给TestCase属性,但得到“属性参数必须是常量表达式、typeof表达式或属性参数类型的数组创建表达式”

这是一个简化版本,但MyStatic是一个返回构建的正则表达式字符串的调用,因此MyStatic中被调用的每个方法都附加到stringbuilder,并具有到字符串的隐式转换

我想保留这种方法,因为如果我创建单独的单元测试,我将违反DRY原则

  [TestCase("","/123",MyStatic.DoThis().And().GetString("ABC"), "id","123")]
  public void MyMehthod(string Root, string Path, string Route, string Param, string Expected)
  {
    var result = SetupRouteResponse(Root, Path, Route, "MatchIt");

    Assert.AreEqual(Expected, (string)result.Context.Parameters[Param]);
  }

尝试对这些类型的参数使用TestCaseSource:

文档中的示例:

 [Test, TestCaseSource("DivideCases")]
 public void DivideTest(int n, int d, int q)
 {
    Assert.AreEqual( q, n / d );
 }

 static object[] DivideCases =
 {
    new object[] { 12, 3, 4 },
    new object[] { 12, 2, 6 },
    new object[] { 12, 4, 3 } 
 };
就你而言:

 [Test, TestCaseSource("MyCaseSource")]
 public void MyMehthod(string Root, string Path, string Route, string Param, string Expected)
 {
   var result = SetupRouteResponse(Root, Path, Route, "MatchIt");

   Assert.AreEqual(Expected, (string)result.Context.Parameters[Param]);
 }

 static object[] MyCaseSource=
 {
    new object[] { "","/123",MyStatic.DoThis().And().GetString("ABC"), "id","123" },
 };