Reputation: 45921
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?
Upvotes: 1
Views: 90
Reputation: 726639
Although you are using the same code, you are using it for different purposes:
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, e.g.
Assert.AreEqual(generatedFile.ProductionOrderName, "actual-order-name");
Assert.AreEqual(generatedFile.BatchName, "actual-batch-name");
... // And so on
Upvotes: 2