将内联变量添加到理论 xUnit [英] Add inline variables to a theory xUnit
问题描述
在使用 xUnit 时,我试图将变量用作 InlineData.
I'm trying to use a variable as InlineData when using xUnit.
看起来像这样:
static string home = "test";
[Theory]
[InlineData(home)]
public void AddTest(string location)
{
Assert.True(LocationIs(location));
}
但我收到错误消息:属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式"
But I'm getting the error: "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"
有什么办法可以让我做我正在尝试的事情吗?还是我只需要对内联数据进行硬编码.
Is there any way for me to do what I'm trying? Or do I need to just hardcode the inline data.
推荐答案
您可以使用 const string home = "test"
来实现,因为那将是一个常量表达式.
You could do it with const string home = "test"
because that would be a constant expression.
您还可以使用 MemberData
属性来指向 yield return new object[]{ "test" };
的内容,这在这方面更加复杂情况下,但在不必保持不变方面更加灵活,并允许您生成许多这样的数组,以便对不同的数据集重复执行测试.
You could also make use of the MemberData
attribute to point to something that does yield return new object[]{ "test" };
which is more convoluted in this case, but more flexible in terms of not having to be constant, and allowing you to yield many such arrays to have the test done repeatedly on different sets of data.
这篇关于将内联变量添加到理论 xUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!