BradleyUffner

Untitled

Jan 20th, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1. public static ISetup<T> SetupByName<T>(this Mock<T> mock, string methodName, params object[] args) where T : class
  2.         {
  3.             var argTypes = args.Select(a => a.GetType()).ToArray();
  4.             var param = Expression.Parameter(typeof(T), "i");
  5.             var method = typeof(T).GetMethod(methodName, argTypes);
  6.             if (method == null) { throw new ArgumentException($"{typeof(T).Name} does not contain a method named \"{methodName}\".", nameof(methodName)); }
  7.             var callArgs = args.Select(a => Expression.Constant(a, a.GetType()));
  8.             var call = Expression.Call(param, method, callArgs);
  9.             var exp = Expression.Lambda<Action<T>>(call, param);
  10.             return mock.Setup(exp);
  11.         }
Advertisement
Add Comment
Please, Sign In to add comment