Pergunta

   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

read table

Here I am using the field as a Key and {length, position, and value} as a 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; }
     }

here I am trying to populate the values so that I can do my assertion

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

       }

But this seams like a redundant code. How can I re-factor it?

Foi útil?

Solução

You can simply use the indexer to get your values, since you have a Dictionary, you don't need to use FirstOrDefault method, just specify the key and then the index of the value:

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

But, I would use a Dictionary<string, SpecFlowData> instead of Dictionary<string, IList<string>> then change the method like this:

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;
}

Then get the values like this:

 string value = data["TRANS ID"].Value;
 string position = data["TRANS ID"].Position;
 string length = data["TRANS ID"].Length; 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top