如何在 TestCaseSource 中使用 FormattableString[]
How to use FormattableString[] in TestCaseSource
我有一个 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);
}
}
我有一个 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); } }