Language: C#
Typemoq 0.1
// ********************************** // 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); } }
Tags:
Description:
the basic code to be able to use Typemock in a moq-like API
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

