SpecFlow and complex objects

前端 未结 7 797
旧巷少年郎
旧巷少年郎 2020-12-23 17:57

I\'m evaluating SpecFlow and I\'m a bit stuck.
All samples I have found are basically with simple objects.

Project I\'m working on heavily relies on a complex ob

7条回答
  •  有刺的猬
    2020-12-23 18:14

    I would say that Marcus is pretty much correct here, however I would write my scenario so that I could use some of the extensions methods for in the TechTalk.SpecFlow.Assist namespace. See here.

    Given I have the following Children:
    | Id | Name | Length |
    | 1  | John | 26     |
    | 2  | Kate | 21     |
    Given I have the following MyObject:
    | Field     | Value      |
    | Id        | 1          |
    | StartDate | 01/01/2011 |
    | EndDate   | 01/01/2011 |
    | Children  | 1,2        |
    

    For the code behind the steps you could use something like this will a bit more error handling in it.

        [Given(@"I have the following Children:")]
        public void GivenIHaveTheFollowingChildren(Table table)
        {
            ScenarioContext.Current.Set(table.CreateSet());
        }
    
    
        [Given(@"I have entered the following MyObject:")]
        public void GivenIHaveEnteredTheFollowingMyObject(Table table)
        {
            var obj = table.CreateInstance();
            var children = ScenarioContext.Current.Get>();
            obj.Children = new List();
    
            foreach (var row in table.Rows)
            {
                if(row["Field"].Equals("Children"))
                {
                    foreach (var childId in row["Value"].Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries))
                    {
                        obj.Children.Add(children
                            .Where(child => child.Id.Equals(Convert.ToInt32(childId)))
                            .First());
                    }
                }
            }
        }
    

    Hope this (or some of this) help to you

提交回复
热议问题