I have a unit test:
[Test]
[TestCaseSource(typeof(AllExampleEnumValues))]
public void GivenSth_WhenSth_ThenSth(ExampleEnum enumValue)
{
// Given
var givenState = BaseClassProperty; // property from fixture's base class
systemUnderTest.SetSomeState(givenState);
// When
var result = systemUnderTest.AnswerAQuestion(
"What's the answer to"
+ " the Ultimate Question of Life,"
+ " the Universe,"
+ " and Everything?");
// Then
Assert.That(result, Is.EqualTo(42));
}
where AllExampleEnumValues
is as follows:
public class AllGranularities : IEnumerable
{
public IEnumerator GetEnumerator()
{
return Enum
.GetValues(typeof(ExampleEnum))
.Cast<ExampleEnum>()
.Select(ee => new object[] { ee })
.GetEnumerator();
}
}
and in the base class I have:
protected int BaseClassProperty
{
get
{
return //depending on enumValue
}
}
How can I get the current value of enumValue
parameter during runtime within the BaseClassProperty
, without changing it to a function and passing enumValue
as parameter?
I know I could somehow walk the stackframe and read it from there (no, actually, I could not: How can I get the values of the parameters of a calling method?), but perhaps there is a more elegant solution?
I need this to improve tests' readability - the fact that enumValue
will influence the result of reading BaseClassProperty
implicitly won't reduce readability, because in my domain this dependency is obvious to everyone.
I've been looking into the TextContext
class, but it seems it has nothing useful in this context. As mentioned in the comments, the value is present in test's name - however parsing the value from a string is not acceptable here, as sometimes we use multiple parameters, and generic test cases.
Another trail leads to using IApplyToTest
attribute. This however would require applying this attribute to every test.
And finally, another trail leads to using ITestAction
, from within I have access to the ITest
interface, which gives a TestMethod
instance. It has the Arguments
property! Unfortunately - it's private.