1

I'm running a Unit test using SpecFlow in VS2013

I have a method called AfterStep which looks like this:

[AfterStep]
private static void AfterStep()
{
    string attribute = ScenarioContext.Current.CurrentScenarioBlock.ToString();
    string extractedValue = GetAttributeValue(attribute);
}

Here is the code for the GetAttributeValue method:

public static string GetAttributeValue(string attribute)
{
        string attrValue = "";
        StackTrace stackTrace = new StackTrace();
        MethodBase method = stackTrace.GetFrame(1).GetMethod();

        switch (attribute)
        {
            case "Given":
                GivenAttribute givenAttr = (GivenAttribute)method.GetCustomAttributes(typeof(GivenAttribute), true)[0];
                attrValue = givenAttr.Regex;
                break;
            case "When":
                WhenAttribute whenAttr = (WhenAttribute)method.GetCustomAttributes(typeof(WhenAttribute), true)[0];
                attrValue = whenAttr.Regex;
                break;
            case "Then":
                ThenAttribute thenAttr = (ThenAttribute)method.GetCustomAttributes(typeof(ThenAttribute), true)[0];
                attrValue = thenAttr.Regex;
                break;
        }

        return attrValue;
}

But it doesnt seem to return the method that was called before the "AfterStep", ie " Step_ConnectToVMS".

Here is the example of the Step_ConnectToVMS step method:

[When(@"I open VMS and connect")]
public static void Step_ConnectToVMS(Table LoginDetails)
{
  //Log into VMS
}

Ive noticed that it works if I call "GetAttributeValue" from within the Step_ConnectToVMS method itself. It just doesnt work if I call it in the "AfterStep" method.

What I essentially want the "AfterStep" method to grab is the the StackFrame for the method that's called before it (in this instance Step_ConnectToVMS) so that I can extract the attribute value.

With these values I can then use them in my custom results logger, which contains extra information (which i'm not at liberty to go into here) but what I really need are these step "descriptions" ie the attribute (Given, When Then) and the value ("I open VMS and connect") to form the headers for this log.

What I do when I get these values is I write them to an XML log, this log then gets picked up by an ASP.NET webserver and generates the page.

As I mentioned in the comments I have a system that works but it involved me putting "GetAttributeValue" in every step method I create. Where as I want it to sit in the "AfterStep" method so that its just in 1 place.

Festivejelly
  • 670
  • 2
  • 12
  • 30
  • 1
    You have posted a solution that doesn't work, not the actual problem you want to solve. you say you want to get the attribute value, but why? what is your ultimate goal? Do you just want the step name? or is there something else you want to do with the value of the attribute? Why do you need it in the `When` method? That seems fishy to me. What problem are you trying to solve, there may be a better solution... – Sam Holder Jan 05 '15 at 19:22
  • Im trying to get which attribute is being used (in this case its "When", but it could be "Then" or "Given" depending on the method) I also want the value of the attribute, which would be "I open VMS and connect". The reason? I built a custom results log which has more features than the standard output, but of course I need this information to make it work properly. For the record the solution does work. Im using it currently. What doesnt work is when I put the code in the "AfterStep" method. – Festivejelly Jan 06 '15 at 08:31
  • 1
    Sorry, I misunderstood the question and thought it worked in the after step but not in the `When` method, but it's obviously the other way round. Please see my answer to [this question](http://stackoverflow.com/questions/26725735/how-to-get-the-current-executing-step-information-in-specflow/26731361#26731361) which may give you what you want (ie the ability to log the current step in the `BeforeStep` or `AfterStep` methods) – Sam Holder Jan 06 '15 at 09:12
  • Cheers Sam ill take a look at that and see if I can get it to work. Much appreciated. – Festivejelly Jan 06 '15 at 13:54
  • 1
    Worked a treat Sam. Upvoted. – Festivejelly Jan 06 '15 at 16:24

0 Answers0