Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpecFlow assist - create instance from table

Tags:

specflow

I'm trying out the specFlow assist and not sure how would one create class property from table. Imagine I have this class:

public class Tracking 
{
    public string Category { get; set; }
}

public class ODARequest
{
    public string Title { get; set; }
    public string Name { get; set; }
    public Tracking Tracking { get; set; }
}

My Given scenario is next:

Scenario: Successfully create an account
Given I have entered the following data into the ODA form:
    | Field                  | Value            |
    | Title                  | Mr               |
    | Name                   | Andy             |
    | Tracking Category      | MDA              |


public void GivenIHaveEnteredTheFollowingDataIntoTheODAForm(Table table)
{
    var request = table.CreateInstance<ODARequest>();
}

The Tracking property will not be populated. Anyone know how to describe Tracking.Category in the table for this situation?

like image 884
Alex M Avatar asked Mar 03 '14 13:03

Alex M


People also ask

Which method is used to create sets from data Table in scenarios?

CreateSet <T> is an extension method off of Table that will convert the table data to a set of objects.

What SpecFlow assist?

Assist Helpers. A number of helpers implemented as extension methods of the Table class make it easier to implement steps that accept a Table parameter. To use these helpers, you need to add the TechTalk.

How do you input a null value into a SpecFlow step Table?

From SpecFlow 3 on-wards, in your Steps class, you can just put the following code. And in the feature file just put null value like this. Now when you use the CreateSet function then it will be deserialized correctly. Show activity on this post.

How do I use scenario context in SpecFlow?

Using the ScenarioContext ScenarioContext is a static class that has a shared state during the execution of a scenario. It can be freely used as a dictionary by either storing an object of a specific type with or without specifying a string key.


1 Answers

I haven't been able to find a way of getting specflow to map "CreateInstance" to non-standard data types properties.

However, in this case you could at least use a StepArgumentTransformation as follows:

    [Given(@"I have entered the following data into the ODA form:")]
    public void GivenIHaveEnteredTheFollowingDataIntoTheODAForm(ODARequest request)
    {
        Assert.IsNotNull(request.Tracking);
    }

    [StepArgumentTransformation(@".*")]
    public ODARequest StringToTracking(Table input)
    {
        return new ODARequest() { 
            Title = input.Rows.Single(row => row["Title"])["value"],
            Name = input.Rows.Single(row => row["Name"])["value"],
            Tracking = new Tracking() 
                { Category = input.Rows.Single(row => row["Field"] == "Tracking Category")["Value"] }
            };
    }

With a little work you could tidy the stepargumenttransformation up to accept each parameter as optional (rather than having "single()" throw if it is omitted).

I feel like there really ought to be a better way to do this, more like your original suggested code. Hope this is helpful.

like image 111
perfectionist Avatar answered Sep 20 '22 17:09

perfectionist