Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static ISetup<T> SetupByName<T>(this Mock<T> mock, string methodName, params object[] args) where T : class
- {
- var argTypes = args.Select(a => a.GetType()).ToArray();
- var param = Expression.Parameter(typeof(T), "i");
- var method = typeof(T).GetMethod(methodName, argTypes);
- if (method == null) { throw new ArgumentException($"{typeof(T).Name} does not contain a method named \"{methodName}\".", nameof(methodName)); }
- var callArgs = args.Select(a => Expression.Constant(a, a.GetType()));
- var call = Expression.Call(param, method, callArgs);
- var exp = Expression.Lambda<Action<T>>(call, param);
- return mock.Setup(exp);
- }
Advertisement
Add Comment
Please, Sign In to add comment