首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在FormattableString[]中使用TestCaseSource

如何在FormattableString[]中使用TestCaseSource
EN

Stack Overflow用户
提问于 2022-05-10 16:29:58
回答 1查看 48关注 0票数 0

我有一个NUnit测试,它需要测试一堆不同的FormattableString用例,并且我使用TestCaseSource传递这些用例

代码语言:javascript
复制
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.Runtime.CompilerServices.FormattableStringFactory+ConcreteFormattableString‘类型的对象不能转换为“System.FormattableString[]”类型。

代码语言:javascript
复制
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

代码语言:javascript
复制
static IEnumerable<TestCaseData> TestCaseSourceData()
{
    FormattableString data1 = FormattableStringFactory.Create("test{0}", 1);
    yield return new TestCaseData(new []{ data1, data1 });
}

我得到了一个不同的错误:

提供的参数太多,最多提供1个参数。

我的密码怎么了?我可以发送任何其他类型的,但不能发送FormattableString

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-10 17:59:57

您正在尝试将FormattableString转换为FormattableString[] --这就是错误消息的意思。

你有两个选择:

  1. 使用单个对象:

TestFixture公共类TestClass {公共静态IEnumerable TestCaseSourceData() { FormattableString data1 = FormattableStringFactory.Create("test{0}",1);收益率返回新TestCaseData(new[] { data1 });} TestCaseSource("TestCaseSourceData")公共空空FormattableStringTest(FormattableString coreMessages) {Assert.True(真);}

  1. 使用对象数组:

TestFixture公共类TestClass {公共静态IEnumerable TestCaseSourceData() { FormattableString data1 = FormattableStringFactory.Create("test{0}",1);屈服返回新TestCaseData(new[] {新FormattableString[] { data1 } });} TestCaseSource("TestCaseSourceData")公共空空FormattableStringTest(FormattableString[] coreMessages) { Assert.AreEqual(1,FormattableString[]);}en19

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72189960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档