Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestCase with list or params

I am trying to write a testcase that takes a string and expects the string split up. I cannot initialize a List in a TestCase, so I tried using TestCaseSource with a params argument, however I get

Wrong number of arguments provided

Is there any way for me to accomplish my end goal?

public IEnumerable<TestCaseData> blah
{
 get
 {
  yield return new TestCaseData("hello World", "h", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d");
 }
}

[TestCaseSource("blah")]
public void testmethod(String orig, params String[] myList)
like image 899
Justin Pihony Avatar asked Jun 20 '13 19:06

Justin Pihony


1 Answers

Even though both your testmethod and TestCaseData constructor take params, TestCaseData interprets params differently: it tries to map them one-to-one to the parameters of the method being tested. In your case, NUnit expects a testmethod with 12 parameters, but your method has only two. This causes the error that you see.

To fix this problem, you need to change the constructor call as follows:

yield return new TestCaseData(
    "hello World"
,   new[] {"h", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"}
);

Now you are passing only two arguments, the second one being an array that must be passed to your params String[] myList.

like image 199
Sergey Kalinichenko Avatar answered Sep 27 '22 21:09

Sergey Kalinichenko