簡體   English   中英

在測試方法和測試方法中使用相同的代碼

[英]Using the same code in test method and in the method tested

我是TDD開發的新手,我剛剛開始使用Nunit 3.7.1,Newtonsoft.Json版本= 10.0.3,C#和.NET Framework 4.7進行一些測試。

我創建了此測試以測試json反序列化:

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

這是我正在測試的方法:

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

文件D:\\trzlexportsample.json具有與string json相同的內容。

我不確定我是否以正確的方式進行測試,因為在測試中,我使用的代碼與_import.LoadBatchFile(path)方法中使用的代碼大致相同

在測試中,我有以下代碼:

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

在測試方法中,我有以下代碼:

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

它們基本相同。

我的方法正確嗎?

換句話說,在測試和測試的方法中使用相同的代碼是否正確?

盡管您使用的是相同的代碼,但是您將其用於不同的目的:

  • 您的代碼使用JSON反序列化器反序列化“有效載荷”字符串,
  • 您的測試代碼使用JSON反序列化器構造一個對象,將針對該對象測試您要測試的程序返回的對象。

如果您正在測試JSON序列化程序代碼本身,這將是有問題的。 但是,您使用的JSON序列化程序庫值得信賴,可以做正確的事情,因此您的方法是完全可以接受的。

顯然,另一種方法是根本不構造exportFile ,而用常量字符串替換對其成員的引用,例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM