У меня есть тест NUnit, который должен проверить кучу разных случаев FormattableString, и я передаю случаи, используя 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)
{
//...
}
Но это дает мне ошибку:
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)
Теперь, если я использую это TestCaseSource:
static IEnumerable<TestCaseData> TestCaseSourceData()
{
FormattableString data1 = FormattableStringFactory.Create("test{0}", 1);
yield return new TestCaseData(new []{ data1, data1 });
}
Я получаю другую ошибку:
Too many arguments provided, provide at most 1 arguments.
Что не так с моим кодом? Я могу отправить любой другой тип, но не FormattableString.





Вы пытаетесь преобразовать FormattableString в FormattableString[] — об этом говорится в сообщении об ошибке.
У вас есть два варианта:
Использовать один объект:
[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);
}
}
Использовать массив объектов:
[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);
}
}
Спасибо! Я совсем забыл, что
new TestCaseData(принимаетparam object[].