【问题标题】:How would I Unit Test these Serialization/Deserialization methods in C#?我将如何在 C# 中对这些序列化/反序列化方法进行单元测试?
【发布时间】:2021-01-06 12:19:33
【问题描述】:

我目前正在尝试为我的一个项目编写一些单元测试,该项目存储有关健身房课程的详细信息列表,但不知道如何执行此操作,因为单元测试对我来说相当新。序列化的方法是 SaveState() 和反序列化的方法是 LoadState() 并且在运行时都可以很好地运行,我只需要编写一些单元测试来快速证明这一点。 allSessions 从中获取的列表(会话)来自另一个保存数据的类,但我不知道在测试时是否真的需要它,或者你是否只是在测试本身中替换了其他东西。非常感谢任何帮助。

    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();
        }

    }

【问题讨论】:

  • 单元测试方法是对文件流使用依赖注入并模拟它。但是,我会质疑这样的测试会带来多少价值。主要是关于测试标准库,而这些测试已经存在。
  • 虽然本身是一个单独的主题并且是一个偏好问题,但 TDD 可以帮助设计主题类,从而识别依赖关系并测试所需的行为。

标签: c# list unit-testing serialization deserialization


【解决方案1】:

单元测试实际上展示了可以以更智能的方式设计的部分代码

我希望您建议重构 SessionsManager 类并添加新的方法来将序列化到流(输入参数)中,例如

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);
}

那么你可以

  • SessionsManager 类中使用这些方法
  • 测试这些方法以检查序列化是否正常工作
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.
  }
}

【讨论】:

  • 谢谢,这是一个很大的帮助。我会努力让这个工作。
  • TestSerialize() 方法不起作用,它表示 SaveState() 中没有与“值”对应的参数
  • @Anthony 看来我忘记输入流参数了,请再试一次。我已经更新了答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2015-02-12
相关资源
最近更新 更多