Passing a string to attribute argument by calling method

前端 未结 1 1578
慢半拍i
慢半拍i 2021-01-21 08:07

I\'m trying to use NUnit and pass in a string argument to the TestCase attribute but I get \"An attribute argument must be a constant expression, typeof expression or array

相关标签:
1条回答
  • 2021-01-21 09:04

    Try using TestCaseSource for those kind of arguments: http://www.nunit.org/index.php?p=testCaseSource&r=2.5.9

    example from the documentation:

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

    in your case:

     [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" },
     };
    
    0 讨论(0)
提交回复
热议问题