SpecFlow assist - create an instance from a table

I am trying to help specFlow and don't know how to create a class property from a table. Imagine that 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 next scenario is as follows:

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. Does anyone know how to describe Tracking.Categorythe table for this situation?

+4
source share
3 answers

I ran into this problem and was thinking how to do it in one step .

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

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

How it works: You can name "CreateInstance" for every complex property you have, so specflow will create an instance for you. Thus, you can have a separate table with properties from different types.

.

, , .

. @Alex M , , . , - .

+3

specflow "CreateInstance" .

StepArgumentTransformation :

    [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"] }
            };
    }

stepargument, ( , "single()" throw, ).

, , . , .

+5

- Given. :

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

:

[Given(@"I have entered the following data into the ODA form:")]
public void GivenIHaveEnteredTheFollowingDataIntoTheODAForm(Table table)
{
    var request = table.CreateInstance<ODARequest>();
    ScenarioContext.Current.Set(request, "request");
}
[Given(@"the tracking info as:")]
public void GivenTheTrackingInfoAs(Table table)
{
    var request = ScenarioContext.Current.Get<ODARequest>("request");
    request.TrackingFields = table.CreateInstance<Tracking>();
}

.

+2

Source: https://habr.com/ru/post/1529802/


All Articles