Click here to Skip to main content
15,885,216 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 48.9K   572   11  
This article explains how to write unit tests for MVVM using Catel.
using System.IO;
using Catel.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Catel.Test.Data
{
	[TestClass]
	public class SavableDataObjectBaseTest
	{
		#region Variables
        private FilesHelper _filesHelper = new FilesHelper();
		#endregion

		#region Initialization and cleanup
		/// <summary>
		/// Cleans up.
		/// </summary>
		[TestCleanup]
		public void CleanUp()
		{
            if (_filesHelper != null)
			{
                _filesHelper.CleanUp();
                _filesHelper = null;
			}
		}
		#endregion

		#region Serialization tests
		/// <summary>
		/// Serializes and deserializes an object using the specified mode. Finally, it will check whether the original object is equal.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="testObject">The test object.</param>
		/// <param name="mode">The mode.</param>
		private void SerializeAndDeserializeObject<T>(T testObject, SerializationMode mode) where T : SavableDataObjectBase<T>
		{
#if SILVERLIGHT
		    SavableDataObjectBase<T> loadedObject;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                // Save
                testObject.Save(memoryStream, mode);

                // Reset position
                memoryStream.Position = 0L;

                // Now read the file again
                loadedObject = SavableDataObjectBase<T>.Load(memoryStream, mode);
            }
#else
			// Get temp filename
            string fileName = _filesHelper.GetTempFileName();

			// Save
			testObject.Save(fileName, mode);

			// Now read the file again
			var loadedObject = SavableDataObjectBase<T>.Load(fileName, mode);
#endif

            // Make sure the objects are equal
			Assert.AreEqual(testObject, loadedObject);
		}

#if !SILVERLIGHT
		[TestMethod]
		public void BinarySerializationLevel1()
		{
			SerializeAndDeserializeObject(DataObjectBaseTestHelper.CreateIniEntryObject(), SerializationMode.Binary);
		}

		[TestMethod]
		public void BinarySerializationLevel2()
		{
			SerializeAndDeserializeObject(DataObjectBaseTestHelper.CreateIniFileObject(), SerializationMode.Binary);
		}

		[TestMethod]
		public void BinarySerializationLevel3()
		{
			SerializeAndDeserializeObject(DataObjectBaseTestHelper.CreateComputerSettingsObject(), SerializationMode.Binary);
		}

		[TestMethod]
		public void BinarySerializationWithPrivateMembers()
		{
			// Create new object
			ObjectWithPrivateMembers objectWithPrivateMembers = new ObjectWithPrivateMembers("My private member");
			objectWithPrivateMembers.PublicMember = "My public member";

			// Test
			SerializeAndDeserializeObject(objectWithPrivateMembers, SerializationMode.Binary);
		}

		[TestMethod]
		public void BinarySerializationWithPrivateParameterlessConstructor()
		{
			// Create new object
			ObjectWithPrivateConstructor objectWithPrivateConstructor = new ObjectWithPrivateConstructor("My private constructor test");

			// Test
			SerializeAndDeserializeObject(objectWithPrivateConstructor, SerializationMode.Binary);
		}
#endif

		[TestMethod]
		public void XmlSerializationLevel1()
		{
			SerializeAndDeserializeObject(DataObjectBaseTestHelper.CreateIniEntryObject(), SerializationMode.Xml);
		}

		[TestMethod]
		public void XmlSerializationLevel2()
		{
			SerializeAndDeserializeObject(DataObjectBaseTestHelper.CreateIniFileObject(), SerializationMode.Xml);
		}

		[TestMethod]
		public void XmlSerializationLevel3()
		{
			SerializeAndDeserializeObject(DataObjectBaseTestHelper.CreateComputerSettingsObject(), SerializationMode.Xml);
		}

		[TestMethod]
		public void XmlSerializationWithXmlMappings()
		{
			SerializeAndDeserializeObject(DataObjectBaseTestHelper.CreateComputerSettingsWithXmlMappingsObject(), SerializationMode.Xml);
		}

        [TestMethod]
        public void XmlSerializationWithCustomTypes()
        {
            // Create object
            ObjectWithCustomType obj = new ObjectWithCustomType();
            obj.FirstName = "Test";
            obj.Gender = Gender.Female;

            // Serialize and deserialize
            SerializeAndDeserializeObject(obj, SerializationMode.Xml);
        }

#if !SILVERLIGHT
		[TestMethod]
		public void XmlSerializationWithPrivateMembers()
		{
			// Create new object
			ObjectWithPrivateMembers objectWithPrivateMembers = new ObjectWithPrivateMembers("My private member");
			objectWithPrivateMembers.PublicMember = "My public member";

			// Test
			SerializeAndDeserializeObject(objectWithPrivateMembers, SerializationMode.Xml);
		}

		[TestMethod]
		public void XmlSerializationWithPrivateParameterlessConstructor()
		{
			// Create new object
			ObjectWithPrivateConstructor objectWithPrivateConstructor = new ObjectWithPrivateConstructor("My private constructor test");

			// Test
			SerializeAndDeserializeObject(objectWithPrivateConstructor, SerializationMode.Xml);
		}
#endif
		#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