Spellchamp
Spellchamp

Reputation: 375

Add inline variables to a theory xUnit

I'm trying to use a variable as InlineData when using xUnit.

It looks like this:

static string home = "test";

[Theory]
[InlineData(home)]
public void AddTest(string location)
{
    Assert.True(LocationIs(location));
}

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.

Upvotes: 6

Views: 5593

Answers (1)

Jon Hanna
Jon Hanna

Reputation: 113342

You could do it with const string home = "test" because that would be a constant expression.

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.

Upvotes: 4

Related Questions