14,217,388 members

38.6K views
11 bookmarked

# Catel - Part 4 of n: Unit testing with Catel

,
Rate this:
28 Jan 2011     CPOL
This article explains how to write unit tests for MVVM using Catel.
 ```﻿// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) 2008 - 2011 Catel development team. All rights reserved. // // // Calculate the product of given value and factor in parameter. // // -------------------------------------------------------------------------------------------------------------------- using System; using System.Windows.Data; namespace Catel.Windows.Data.Converters { /// /// Calculate the product of given value and factor in parameter. /// #if !SILVERLIGHT [ValueConversion(typeof(int), typeof(int))] #endif public class MultiplyConverter : IValueConverter { /// /// Calulate the product of given value and factor in parameter. /// /// Base value. /// Not used, supports now int and double. /// Factor used for multiply (always double). /// Not used. /// Product of specified values. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // Get typed value double typedValue = 0d; // Check input if (value is int) { typedValue = System.Convert.ToDouble((int)value); } else if (value is double) { typedValue = (double)value; } // Check value if (typedValue == 0d) { return 0d; } // Get factor double factor; if (!double.TryParse(parameter as string, out factor)) { return 0d; } // Multiply return typedValue * factor; } /// /// Calculate value back. /// /// Base value. /// Not used, supports now int and double. /// Factor used for multiply (always double). /// Not used. /// Original value. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // Get typed value double typedValue = 0d; // Check input if (value is int) { typedValue = System.Convert.ToDouble((int)value); } else if (value is double) { typedValue = (double)value; } // Check value if (typedValue == 0d) { return 0d; } // Get factor double factor; if (!double.TryParse(parameter as string, out factor)) { return 0d; } // Divide return typedValue / factor; } } } ```

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.