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

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

这是一个简化版本,但MyStatic是一个返回一个构建的RegEx字符串的调用,因此被调用的MyStatic中的每个方法都附加到stringbuilder并且隐式转换为string。

我想保留这种方法,因为如果我创建单独的unit testing,我将违反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来获取这些参数: http ://www.nunit.org/index.php?p = testCaseSource&r = 2.5.9

文档中的示例:

  [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" }, };