2017-08-02 100 views
1

我是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.jsonstring 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)); 
} 

他們大多是相同的。

我這樣做的方式是否正確?

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

回答

2

雖然使用的是相同的代碼,您使用的是它爲不同的目的:

  • 代碼使用JSON解串器反序列化「有效載荷」的字符串,
  • 您的測試代碼使用JSON解串器構建一個對象,你要測試你正在測試的程序返回的對象。

如果您正在測試JSON序列化程序代碼本身,這會有問題。但是,您正在使用您信任的JSON序列化程序庫來執行正確的操作,因此您的方法完全可以接受。

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

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