I have a method that serializes a collection to a file using the XmlSerializer.
public void Save(List<RetryAttempt> retryAttempts)
{
FileStream fs = new FileStream(this.fileName, FileMode.Create);
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<RetryAttempt>));
xmlSerializer.Serialize(fs, retryAttempts);
}
catch (Exception ex)
{
LocalLogger.LogError("Unable to save retry information to xml file.", ex.ToString());
}
finally
{
fs.Close();
}
}
I then have another method that deserializes the collection back from the file
public List<RetryAttempt> GetRetryAttempts()
{
List<RetryAttempt> retryAttempts = new List<RetryAttempt>();
if (File.Exists(this.fileName))
{
FileStream fs = new FileStream(this.fileName, FileMode.Open);
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<RetryAttempt>));
retryAttempts = (List<RetryAttempt>)xmlSerializer.Deserialize(fs);
}
catch (Exception ex)
{
LocalLogger.LogError("Unable to read from retry xml file.", ex.ToString());
}
finally
{
fs.Close();
}
}
return retryAttempts;
}
So far so good (unless anyone spots something glaringly wrong with that code…). However, my unit test for this now fails
[Test]
public void GetRetryAttempts_AttemptsExist_ListOfAttemptsReturned()
{
this.attempt = new RetryAttempt("1234", 4);
this.attempts = new List<RetryAttempt>() { attempt };
this.xmlStore = new XmlRetryFileStore(RetryType.Download);
xmlStore.Save(attempts);
List<RetryAttempt> savedAttempts = xmlStore.GetRetryAttempts();
Assert.Contains(attempt, savedAttempts);
}
I would expect the list of my custom object to contain the “attempt” that I serialized to the file. Instead I am getting the following failure.
Expected: collection containing < MyNamespace.RetryManagement.RetryAttempt >
But was: < MyNamespace.RetryManagement.RetryAttempt >
This message seems to suggest that instead of a collection being returned, just one object was returned. This is obviously not the case – there is a List being returned that only contains one element – I can see the collection in the immediate window and everything looks fine. I can run the test with the debugger and everything seems okay. If I simply create a collection and assert that it contains the element without saving and getting it from XML then it works fine so the point of failure must be around the serialization?
I would suspect that your class doesn’t implement its own overridden version of
.Equalsmethod. The contains method would otherwise do a reference check and since you are comparing serialized versions with the original the instances are not the same.I would suggest implementing this (along with
GetHashCodeso you are able to do comparisons for equality