Home > Blockchain >  How to use FormattableString[] in TestCaseSource
How to use FormattableString[] in TestCaseSource

Time:05-11

I have an NUnit test that needs to test a bunch of different FormattableString cases and I'm passing the cases using TestCaseSource:

static IEnumerable<TestCaseData> TestCaseSourceData()
{
    FormattableString data1 = FormattableStringFactory.Create("test{0}", 1);
    yield return new TestCaseData(new []{ data1 });
}

[TestCaseSource("TestCaseSourceData")]
public void FormattableStringTest(FormattableString[] coreMessages)
{
    //...
}

But this give me an error:

System.ArgumentException : Object of type 'System.Runtime.CompilerServices.FormattableStringFactory ConcreteFormattableString' cannot be converted to type 'System.FormattableString[]'.

System.ArgumentException : Object of type 'System.Runtime.CompilerServices.FormattableStringFactory ConcreteFormattableString' cannot be converted to type 'System.FormattableString[]'.
   at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.CheckArguments(StackAllocedArguments& stackArgs, ReadOnlySpan`1 parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at NUnit.Framework.Internal.Reflect.InvokeMethod(MethodInfo method, Object fixture, Object[] args)

Now if I use this TestCaseSource:

static IEnumerable<TestCaseData> TestCaseSourceData()
{
    FormattableString data1 = FormattableStringFactory.Create("test{0}", 1);
    yield return new TestCaseData(new []{ data1, data1 });
}

I get a different error:

Too many arguments provided, provide at most 1 arguments.

What's wrong with my code? I can send any other type but not FormattableString.

CodePudding user response:

You are trying to cast FormattableString to FormattableString[] - this is what error message is saying.

You have two options:

  1. Use single object:

     [TestFixture]
     public class TestClass
     {
         public static IEnumerable<TestCaseData> TestCaseSourceData()
         {
             FormattableString data1 = FormattableStringFactory.Create("test{0}", 1);
             yield return new TestCaseData(new[] { data1 });
         }
    
         [TestCaseSource("TestCaseSourceData")]
         public void FormattableStringTest(FormattableString coreMessages)
         {
             Assert.True(true);
         }
     }
    
  2. Use array of objects:

     [TestFixture]
     public class TestClass
     {
         public static IEnumerable<TestCaseData> TestCaseSourceData()
         {
             FormattableString data1 = FormattableStringFactory.Create("test{0}", 1);
             yield return new TestCaseData(new[] { new FormattableString[] { data1 } });
         }
    
         [TestCaseSource("TestCaseSourceData")]
         public void FormattableStringTest(FormattableString[] coreMessages)
         {
             Assert.AreEqual(1, coreMessages.Length);
         }
     }
    
  • Related