Click here to Skip to main content
15,896,526 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 49.5K   572   11  
This article explains how to write unit tests for MVVM using Catel.
using System;
using System.IO;
using System.Runtime.Serialization;
using Catel.Runtime.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#if !SILVERLIGHT
using System.Runtime.Serialization.Formatters.Binary;
#endif

namespace Catel.Test.Runtime.Serialization
{
    /// <summary>
    /// Summary description for SerializationHelperTest
    /// </summary>
    [TestClass]
    public class SerializationHelperTest
    {
        #region Test classes
#if !SILVERLIGHT
        [Serializable]
#else
        [DataContract]
#endif
        internal class SerializableObject 
#if !SILVERLIGHT
            : ISerializable
#endif
        {
            #region Variables
            private string _stringValue;
            private int _intValue;
            private bool _boolValue;
            private DateTime _objectValue;
            #endregion

            #region Constructor & destructor
            public SerializableObject()
            {
                // Set default values
                _stringValue = "default string";
                _intValue = 5;
                _boolValue = true;
                _objectValue = DateTime.Now;
            }
            #endregion

            #region Properties
            public string StringValue
            {
                get
                {
                    return _stringValue;
                }
                set
                {
                    _stringValue = value;
                }
            }

            public int IntValue
            {
                get
                {
                    return _intValue;
                }
                set
                {
                    _intValue = value;
                }
            }

            public bool BoolValue
            {
                get
                {
                    return _boolValue;
                }
                set
                {
                    _boolValue = value;
                }
            }

            public DateTime ObjectValue
            {
                get
                {
                    return _objectValue;
                }
                set
                {
                    _objectValue = value;
                }
            }
            #endregion

            #region Methods
            #endregion

            #region Serialization
#if !SILVERLIGHT
            public SerializableObject(SerializationInfo info, StreamingContext context)
            {
                // Read values
                _stringValue = SerializationHelper.GetString(info, "StringValue", "default string");
                _intValue = SerializationHelper.GetInt(info, "IntValue", 5);
                _boolValue = SerializationHelper.GetBool(info, "BoolValue", true);
                _objectValue = (DateTime)SerializationHelper.GetObject(info, "ObjectValue", typeof(DateTime), DateTime.Now);
            }

            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                // Store values
                info.AddValue("StringValue", _stringValue);
                info.AddValue("IntValue", _intValue);
                info.AddValue("BoolValue", _boolValue);
                info.AddValue("ObjectValue", _objectValue);
            }
#endif
            #endregion
        }
        #endregion

        [TestMethod]
        public void TestSerialization()
        {
            // Declare variables
            MemoryStream stream = null;

            // Create object
            SerializableObject obj = new SerializableObject();

            // Set some values
            obj.StringValue = "SerializationHelperTest";
            obj.IntValue = 1;
            obj.BoolValue = false;
            obj.ObjectValue = DateTime.MinValue;

            // Create formatter
#if SILVERLIGHT
            DataContractSerializer serializer = SerializationHelper.GetDataContractSerializer(obj.GetType(), "test", obj, false); 
#else
            BinaryFormatter serializer = new BinaryFormatter();
#endif

            #region Serialize to disk
            // Create stream
            stream = new MemoryStream();

            // Serialize
#if SILVERLIGHT
            serializer.WriteObject(stream, obj);
#else
            serializer.Serialize(stream, obj);
#endif
            #endregion

            #region Deserialize from disk
            // Reset stream position
            stream.Position = 0L;

            // Serialize
#if SILVERLIGHT
            obj = (SerializableObject)serializer.ReadObject(stream);
#else
            obj = (SerializableObject)serializer.Deserialize(stream);
#endif

            // Close stream
            stream.Close();
            #endregion

            // Test values
            Assert.AreEqual(obj.StringValue, "SerializationHelperTest");
            Assert.AreEqual(obj.IntValue, 1);
            Assert.AreEqual(obj.BoolValue, false);
            Assert.AreEqual(obj.ObjectValue, DateTime.MinValue);
        }

        #region DeserializeXml
        // TODO: Write unit test
        #endregion

        #region DeserializeXml
        // TODO: Write unit test
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions