Click here to Skip to main content
15,897,094 members
Articles / DevOps / Unit Testing

Using partial mocks for non public methods testing

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
10 Feb 2013CPOL3 min read 44.8K   174   7  
Pattern to follow using mock frameworks so non public methods can be tested. The code example is based on the RhinoMock framework but it is easily applicable to any other mock framework.
namespace MockByExample.Calculators
{
    /// <summary>
    /// Determines the business <see cref="RuleType"/> from a given integer value
    /// </summary>
    public class BusinessRuleCalculator
    {
        /// <summary>
        /// Determines the business <see cref="RuleType"/> from
        /// a passed value
        /// </summary>
        /// <param name="value">Integer value to determine the business value from</param>
        /// <returns>
        /// Zero if value is zero,
        /// see <see cref="GetRuleForNegative"/> for a negative value, 
        /// see <see cref="GetRuleForPositive"/> for a positive value
        /// </returns>
        public  RuleType GetRule(int value)
        {
            if(value == 0) return RuleType.Zero;
            return value > 0
                       ? GetRuleForPositive(value)
                       : GetRuleForNegative(value);
        }

        /// <summary>
        /// Determines the business rule type for a negative value
        /// </summary>
        /// <param name="value">Negative value to determine the rule type</param>
        /// <returns>
        /// OverThreshold if the value is less than -10, 
        /// NegativeAnomaly if the value is between -6 and -8 (inclusive), 
        /// otherwise NegativeNormalCase
        /// </returns>
        protected virtual RuleType GetRuleForNegative(int value)
        {
            if(value < -10) return RuleType.OverThreshold;
            return value.IsBetween(-6, -8)
                ? RuleType.NegativeAnomaly
                : RuleType.NegativeNormalCase;
        }

        /// <summary>
        /// Determines the business rule type for a positive value
        /// </summary>
        /// <param name="value">Positive value to determine the rule type</param>
        /// <returns>
        /// OverThreshold if the value is greater than 10, 
        /// PositiveAnomaly if the value is between 3 and 5 (inclusive), 
        /// otherwise PositiveNormalCase
        /// </returns>
        protected virtual RuleType GetRuleForPositive(int value)
        {
            if(value > 10) return RuleType.OverThreshold;
            return value.IsBetween(3, 5) 
                ? RuleType.PositiveAnomaly 
                : RuleType.PositiveNormalCase;
        }
    }
}

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 (Senior)
Ireland Ireland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions