CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Typemoq 0.1

687 Views
Copy Code Show/Hide Line Numbers
// **********************************
// By Roy Osherove : www.osherove.com
// see example usage of this code at http://weblogs.asp.net/rosherove/archive/2009/10/23/typemoq-api.aspx 
// **********************************
 
using System;
using System.Linq;
using System.Linq.Expressions;
using TypeMock.ArrangeActAssert;
 
namespace Typemoq
{
    public class  Faker:Faker<Faker>
    {
        public static IPublicVoidActionHandler Setup(Action action)
        {
            IPublicVoidActionHandler handler = Isolate.WhenCalled(action);
            return handler;
        }
    }
    public class Faker<T> : IBehavior
    {
        
        private INonPublicMethodHandler pendingHandler;
 
        public Faker()
        {
            Object = Isolate.Fake.Instance<T>();
        }
 
        public T Object { get; set; }
 
        public IBehavior Method(Expression<Action<T>> action)
        {
            var expression = action.ToLambda();
            string methodName = expression.ToMethodCall().Method.Name;
 
            INonPublicMethodHandler handler = Isolate.NonPublic.WhenCalled(Object, methodName);
            pendingHandler = handler;
            return this;
        }
        
        public void VerifyWithAnyArgs(Expression<Action<T>> action)
        {
            var expression = action.ToLambda();
            string methodName = expression.ToMethodCall().Method.Name;
 
            Isolate.Verify.NonPublic.WasCalled(Object, methodName);
        }
 
        public void Throws(Exception exception)
        {
            pendingHandler.WillThrow(exception);
        }
 
        public void Returns(object retVal)
        {
            pendingHandler.WillReturn(retVal);
        }
    }
 
 
    internal static class ExpressionExtensions {
        /// <summary>
        /// Casts the expression to a lambda expression, removing 
        /// a cast if there's any.
        /// </summary>
        public static LambdaExpression ToLambda(this Expression expression) {
 
            LambdaExpression lambda = expression as LambdaExpression;
            if (lambda == null)
                //                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
                //                                                          Properties.Resources.UnsupportedExpression, expression));
 
                return null;
            // Remove convert expressions which are passed-in by the MockProtectedExtensions.
            // They are passed because LambdaExpression constructor checks the type of 
            // the returned values, even if the return type is Object and everything 
            // is able to convert to it. It forces you to be explicit about the conversion.
            var convert = lambda.Body as UnaryExpression;
            if (convert != null && convert.NodeType == ExpressionType.Convert)
                lambda = Expression.Lambda(convert.Operand, lambda.Parameters.ToArray());
 
            return lambda;
        }
 
        /// <summary>
        /// Casts the body of the lambda expression to a <see cref="MethodCallExpression"/>.
        /// </summary>
        /// <exception cref="ArgumentException">If the body is not a method call.</exception>
        public static MethodCallExpression ToMethodCall(this LambdaExpression expression) {
            var methodCall = expression.Body as MethodCallExpression;
            if (methodCall == null) {
                return null;
            }
 
            return methodCall;
        }
 
 
 
 
    }
 
 
    public interface IBehavior {
        void Throws(Exception exception);
        void Returns(object retVal);
    }
}
by Roy Osherove
  October 23, 2009 @ 6:08am
Tags:
Description:
the basic code to be able to use Typemock in a moq-like API

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate