Skip to content

Instantly share code, notes, and snippets.

@jwChung
Created September 7, 2014 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwChung/3487aab84044d104b745 to your computer and use it in GitHub Desktop.
Save jwChung/3487aab84044d104b745 to your computer and use it in GitHub Desktop.
Mocked.cs
namespace Jwc.Experiment.Xunit
{
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Moq;
public static class Mocked
{
private static readonly Type TypeOfMockQueryable
= typeof(Mock).Assembly.GetTypes().Single(t => t.Name == "MockQueryable`1");
public static T Of<T>(this T mocked, Expression<Func<T, bool>> predicate) where T : class
{
return CreateMockQuery<T>(mocked).First(predicate);
}
public static T Of<T>(Expression<Func<T, bool>> predicate) where T : class
{
return Of<T>().Of(predicate);
}
public static T Of<T>() where T : class
{
return new Mock<T> { DefaultValue = DefaultValue.Mock }.Object;
}
public static Mock<T> ToMock<T>(this T mocked) where T : class
{
return Mock.Get(mocked);
}
private static IQueryable<T> CreateMockQuery<T>(T mocked) where T : class
{
return (IQueryable<T>)Mocked.GetConstructorOfMockQueryable<T>()
.Invoke(new object[] { Mocked.GetMethodCallExpression(mocked) });
}
private static ConstructorInfo GetConstructorOfMockQueryable<T>()
{
return Mocked.TypeOfMockQueryable
.MakeGenericType(typeof(T))
.GetConstructor(new[] { typeof(MethodCallExpression) });
}
private static MethodCallExpression GetMethodCallExpression<T>(T mocked) where T : class
{
return Expression.Call(
new Func<T, IQueryable<T>>(m => new[] { m }.AsQueryable()).Method,
Expression.Constant(mocked));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment