Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

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

    }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...