【问题标题】:How to re-factor a spec flow data table and a Test Context properties如何重构 specflow 数据表和 Testcontext 属性
【发布时间】:2014-04-01 08:13:38
【问题描述】:
   Field                  Length    position     Value
TRANS LENGTH            4          5            1234
TRANS DEST              7          9           DEV
TRANS ORGN              7          16          PROD
TRANS DATE              6          23          2014-03-30
TRANS ID                4          35          44

读表

这里我使用字段作为 Key 和 {length, position, and value} 作为值

public class SpecFlowData
{
    public string Field { get; set; }
    public string Value { get; set; }
    public string Position { get; set; }
    public string Length { get; set; }

     public Dictionary<string,  SpecFlowData> GetData(Table table)
    {
     var data = table.CreateSet<SpecFlowData>().ToList();
       var result =  data
        .ToDictionary(x => x.Field, x => new SpecFlowData
        {
            Value = x.Value,
            Position = x.Position, 
            Length = x.Length
        });
    return result;
    }
     }


  public class TestContext
     {
    public SpecFlowData TRANSLENGETH{ get; set; }
    public SpecFlowData TRANSDEST{ get; set; }
   public SpecFlowData TRANSORGN { get; set; }
    public SpecFlowData TRANSDATE { get; set; }
    public SpecFlowData TRANSID { get; set; }
     }

我在这里尝试填充值以便我可以做我的断言

  [Given(@"Request is sent with the following data:")]
  public void GivenRequestIsSentWithTheFollowingData(Table table)
  {
     var data = SpecFlowData.GetRequestData(table);
        TestContext.TRANSLENGETH= data["TRANSLENGETH"];
        TestContext.TRANSDEST= data["TRANSDEST"];
        TestContext.TRANSORGN = data["TRANSORGN "];

   }

     [Then(@"Response will contain the expected data:")]
    public void ThenResponseWillContainTheExpectedData()

     {

        var Response = "HERE I HAVE the string response message that I am asserting ";

          Assert.AreEqual(Response.Substring(TestContext.TransLength.Position,  TestContext.TransLength.Length), TestContext.TransLength);
    Assert.AreEqual(Response.Substring(TestContext.TransDest.Position,      TestContext.TransDest.Length), TestContext.TransDest);
      ...... more TestContext properties to be asserted.

       }

但这看起来像是一个冗余代码。我怎样才能重新考虑它?

【问题讨论】:

    标签: c# .net dictionary key-value


    【解决方案1】:

    你可以简单地使用索引器来获取你的值,因为你有一个字典,你不需要使用FirstOrDefault 方法,只需指定键,然后是值的索引:

    string value = data["TRANS ID"][0];
    string position = data["TRANS ID"][1];
    string length = data["TRANS ID"][2]; 
    

    但是,我会使用Dictionary&lt;string, SpecFlowData&gt; 而不是Dictionary&lt;string, IList&lt;string&gt;&gt;,然后像这样更改方法:

    public Dictionary<string,  SpecFlowData> GetData(Table table)
    {
        var data = table.CreateSet<SpecFlowData>().ToList();
        var result =  data
                .ToDictionary(x => x.Field, x => new SpecFlowData
                {
                    Value = x.Value,
                    Position = x.Position, 
                    Length = x.Length
                });
       return result;
    }
    

    然后得到这样的值:

     string value = data["TRANS ID"].Value;
     string position = data["TRANS ID"].Position;
     string length = data["TRANS ID"].Length; 
    

    【讨论】:

    • 谢谢你我接受你的回答...非常干净的建议。
    • 我已将我的问题标记为已回答,但我仍有一个相关问题。
    • 我接受了这个答案,因为它回答了我的问题。但我还有一个与此相关的问题。
    • @HXD 你可以问另一个问题,但当然不是在 cmets =)
    • 如果您有其他问题,请提出其他问题,请勿编辑您的帖子
    猜你喜欢
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多