Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何重新考虑等级库流数据表和测试上下文属性_C#_.net_Dictionary_Key Value - Fatal编程技术网

C# 如何重新考虑等级库流数据表和测试上下文属性

C# 如何重新考虑等级库流数据表和测试上下文属性,c#,.net,dictionary,key-value,C#,.net,Dictionary,Key Value,阅读表格 这里我使用字段作为键,{length,position,and value}作为值 Field Length position Value TRANS LENGTH 4 5 1234 TRANS DEST 7 9 DEV TRANS ORGN 7 16

阅读表格

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

   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

但这就像是一个冗余代码。我如何重新考虑它

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

  [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.

       }
但是,我会使用
字典
而不是
字典
,然后更改如下方法:

string value = data["TRANS ID"][0];
string position = data["TRANS ID"][1];
string length = data["TRANS ID"][2]; 
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;
}

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

  [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.

       }
但是,我会使用
字典
而不是
字典
,然后更改如下方法:

string value = data["TRANS ID"][0];
string position = data["TRANS ID"][1];
string length = data["TRANS ID"][2]; 
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;
}

谢谢,我接受你的回答…非常干净的建议。我标记了我的问题的答案,但我还有一个相关的问题。我接受了这个答案,因为它回答了我的问题。但我还有一个问题与此相关。@HXD您可以自由提出另一个问题,但当然不能在评论中=)如果您有其他问题,请提出另一个问题,不要编辑你的帖子谢谢你我接受你的答案…非常干净的建议。我标记了我的问题的答案,但我还有一个相关的问题。我接受了这个答案,因为它回答了我的问题。但是我还有一个问题与此相关。@HXD你可以自由地问另一个问题,但当然不能在评论中=)如果你有另一个问题,请问另一个问题,不要编辑你的帖子