Beispiel #1
0
        public static MockedIdentityDbContext <T, TU> GetIdentityMockContext <T, TU>() where T : IdentityDbContext <TU> where TU : IdentityUser
        {
            var instance = new MockedIdentityDbContext <T, TU>();

            instance.MockTables();
            return(instance);
        }
Beispiel #2
0
        public static void MockTables <T, TU>(this MockedIdentityDbContext <T, TU> mockedContext) where T : IdentityDbContext <TU> where TU : IdentityUser
        {
            Type contextType     = typeof(T);
            var  dbSetProperties = contextType.GetProperties().Where(prop => (prop.PropertyType.IsGenericType) && (prop.PropertyType.GetGenericTypeDefinition() == typeof(DbSet <>) || prop.PropertyType.GetGenericTypeDefinition() == typeof(IDbSet <>)));

            foreach (var prop in dbSetProperties)
            {
                var  dbSetGenericType = prop.PropertyType.GetGenericArguments()[0];
                Type listType         = typeof(List <>).MakeGenericType(dbSetGenericType);
                var  listForFakeTable = Activator.CreateInstance(listType);
                var  parameter        = Expression.Parameter(contextType);
                var  body             = Expression.PropertyOrField(parameter, prop.Name);
                var  lambdaExpression = Expression.Lambda <Func <T, object> >(body, parameter);
                var  method           = typeof(EntityFrameworkMockHelper).GetMethod("MockDbSet").MakeGenericMethod(dbSetGenericType);
                mockedContext.Setup(lambdaExpression).Returns(method.Invoke(null, new[] { listForFakeTable }));
                mockedContext.Tables.Add(prop.Name, listForFakeTable);
            }
        }