[英]How would I Unit Test these Serialization/Deserialization methods in C#?
I am currently trying to write some Unit Tests for a project of mine that stores lists of details about sessions in a gym and have no idea how to do so as Unit Testing is rather new to me.我目前正在尝试为我的一个项目编写一些单元测试,该项目存储有关健身房会话的详细信息列表,并且不知道如何这样做,因为单元测试对我来说相当新。 The method to serialize is SaveState() and the method to deserialize is LoadState() and both work perfectly fine when ran thankfully, I just need to write some unit tests to prove that at quickly.序列化的方法是 SaveState() 和反序列化的方法是 LoadState() 并且在运行时都可以正常工作,我只需要编写一些单元测试来快速证明这一点。 The list(session) that allSessions takes from is from another class that holds the data but I don't know if that is actually needed when testing or if you just substitute something else in within the test itself. allSessions 从中获取的列表(会话)来自另一个保存数据的 class 但我不知道在测试时是否真的需要它,或者您是否只是在测试本身中替换其他东西。 Any help is greatly appriciated.非常感谢任何帮助。
public class SessionsManager
{
const string FILENAME = "SessionFile.dat";
//declare private list for events
private List<Session> allSessions;
// Public Property
public List<Session> AllSessions { get => allSessions; set => allSessions = value; }
//creating constructor to hold lists
public SessionsManager()
{
AllSessions = new List<Session>();
}
public void SaveState()
{
//Formatter object
BinaryFormatter biFormatter = new BinaryFormatter();
//stream object to create file types
FileStream outFile = new FileStream(FILENAME, FileMode.Create, FileAccess.Write);
//Save the whole list in one
biFormatter.Serialize(outFile, allSessions);
//Close the stream, dont want them crossing after all
outFile.Close();
}
public void LoadState()
{
//Formatter object
BinaryFormatter biiFormatter = new BinaryFormatter();
//stream object to read file
FileStream InFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
//Deserialise the whole list
allSessions = ((List<Session>)biiFormatter.Deserialize(InFile));
//close stream
InFile.Close();
}
}
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我希望您建议重构SessionsManager
class 并添加将序列化到 stream (输入参数)的新方法,例如
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那么你也能
SessionsManager
class在SessionsManager
class 中使用这些方法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.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.