How to pass in a method as an expression using attributes?
I want to decorate by the attribute a test method. This attribute should
know about what test is considered to be previous with regard to the
attributed method. Here is an exampel:
private void
DestinationChoiceViewLoaded_AllTpmFeaturesAreEnabled_UIElementsAreSetUpProperly()
{
var vut = new DestinationChoiceViewUIWrapper();
UIControlAssert.Clickable(vut.GetNextPageButton());
UIControlAssert.Clickable(vut.GetPreviousPageButton());
}
[PreviousTestInOrder()]
public void Run_DestinationChoiceView_Tests() {
var vut = new DestinationChoiceViewUIWrapper();
UIControlAssert.Clickable(vut.GetNextPageButton());
UIControlAssert.Clickable(vut.GetPreviousPageButton());
}
Somehow, I want to pass in
DestinationChoiceViewLoaded_AllTpmFeaturesAreEnabled_UIElementsAreSetUpProperly
to the attribute constructor.
I tried:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited
= false)]
public class PreviousTestInOrderAttribute : Attribute
{
public string MethodName { get; private set; }
public PreviousTestInOrderAttribute(Expression<Action>
memberExpression) {
MethodName = GetMemberName(memberExpression);
}
public static string GetMemberName(Expression<Action> memberExpression) {
MemberExpression expressionBody = (MemberExpression)
memberExpression.Body;
return expressionBody.Member.Name;
}
}
But if I do
[PreviousTestInOrder(() =>
DestinationChoiceViewLoaded_AllTpmFeaturesAreEnabled_UIElementsAreSetUpProperly)]
it won't compile :(