unit testing actually shows parts of the code that can be designed in more smart way
I'd like you to suggest to refactor the SessionsManager
class and add new methods that will do serialization into stream (input parameter) like
public static T LoadState<T>(Stream stream)
{
BinaryFormatter biiFormatter = new BinaryFormatter();
return (T)biiFormatter.Deserialize(stream);
}
public static void SaveState<T>(Stream stream, T value)
{
BinaryFormatter biFormatter = new BinaryFormatter();
biFormatter.Serialize(outFile, value);
}
then you can
- use those methods within
SessionsManager
class
- test those methods to check is serialization working
public void TestSerialize()
{
using (var ms = new MemoryStream())
{
SessionsManager.SaveState(ms, new List<Session>() { new Session() } );
ms.Position = 0;
var res = SessionManager.LoadState<List<Session>>(ms);
Assert.AreEqual(1, res.Count); // check that there are the same count of elements e.g.
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…