简体   繁体   中英

Using the same code in test method and in the method tested

I'm new in TDD developing and I've just started to do some tests with Nunit 3.7.1, Newtonsoft.Json version=10.0.3, C# and .NET Framework 4.7.

I have created this test to test a json deserialization:

[Test]
public void ShouldGenerateBatchExportFile()
{
    string json = @"{""ProductionOrderName"": ""proOrd"",""BatchName"": ""batch_01"",""Codes"": [ --- OMITTED FOR BREVETY --- ]}";
    string path = @"d:\trzlexportsample.json";
    BatchExportFile exportFile = null;

    exportFile = JsonConvert.DeserializeObject<BatchExportFile>(json);

    BatchExportFile generatedFile = _import.LoadBatchFile(path);

    Assert.AreEqual(generatedFile.ProductionOrderName, exportFile.ProductionOrderName);
    Assert.AreEqual(generatedFile.BatchName, exportFile.BatchName);

    Assert.That(generatedFile.Codes, Has.Count.EqualTo(exportFile.Codes.Count));
    Assert.That(generatedFile.Aggregations, Has.Count.EqualTo(exportFile.Aggregations.Count));

    for(int index = 0; index < generatedFile.Codes.Count; index++)
    {
        CodeData gData = generatedFile.Codes[index];
        CodeData testData = exportFile.Codes[index];

        Assert.AreEqual(gData.CodeId, testData.CodeId);
        Assert.AreEqual(gData.Serial, testData.Serial);
        Assert.AreEqual(gData.AggregationLevelId, testData.AggregationLevelId);
        Assert.AreEqual(gData.CommissioningFlag, testData.CommissioningFlag);
        Assert.AreEqual(gData.LastChange, testData.LastChange);
        Assert.AreEqual(gData.UserName, testData.UserName);
        Assert.AreEqual(gData.Source, testData.Source);
        Assert.AreEqual(gData.Reason, testData.Reason);
    }

    for (int index = 0; index < generatedFile.Aggregations.Count; index++)
    {
        AggregationData gData = generatedFile.Aggregations[index];
        AggregationData testData = generatedFile.Aggregations[index];

        Assert.AreEqual(gData.AggregationId, testData.AggregationId);
        Assert.AreEqual(gData.Parent, testData.Parent);
        Assert.That(gData.Children, Has.Count.EqualTo(testData.Children.Count));

        for (int j = 0; j < gData.Children.Count; j++)
        {
            AggregationChildrenData gChildren = gData.Children[j];
            AggregationChildrenData testChildren = testData.Children[j];

            Assert.AreEqual(gChildren.Serial, testChildren.Serial);
            Assert.AreEqual(gChildren.Serial, testChildren.Serial);
        }
    }
}

And this is the method I'm testing:

public BatchExportFile LoadBatchFile(string path)
{
    if (string.IsNullOrWhiteSpace(path))
        throw new ArgumentNullException(nameof(path));

    BatchExportFile exportFile = null;

    using (StreamReader file = File.OpenText(path))
    {
        JsonSerializer serializer = new JsonSerializer();
        exportFile = (BatchExportFile)serializer.Deserialize(file, typeof(BatchExportFile));
    }

    return exportFile;
}

The file D:\\trzlexportsample.json has the same content than the string json .

I'm not sure if I'm doing the test in the right way because in the test I'm using mostly the same code than in the method _import.LoadBatchFile(path) .

In the test I have this code:

exportFile = JsonConvert.DeserializeObject<BatchExportFile>(json);

And in the test method I have this code:

using (StreamReader file = File.OpenText(path))
{
    JsonSerializer serializer = new JsonSerializer();
    exportFile = (BatchExportFile)serializer.Deserialize(file, typeof(BatchExportFile));
}

They are mostly the same.

Is the way I do it correct?

In other words, is it correct to use the same code in the test and in the method tested?

Although you are using the same code, you are using it for different purposes:

  • Your code uses JSON deserializer to deserialize the "payload" string,
  • Your test code uses JSON deserializer to construct an object against which you are going to test the object returned by the program that you are testing.

This would be problematic if you were testing JSON serializer code itself. However, you are using JSON serializer library that you trust to do the right thing, so your approach is perfectly acceptable.

Obviously, another approach would be to not construct exportFile at all, and replace references to its members with constant strings, eg

Assert.AreEqual(generatedFile.ProductionOrderName, "actual-order-name");
Assert.AreEqual(generatedFile.BatchName, "actual-batch-name");
... // And so on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM