/// <summary> /// Gets the mocked set for a given entity type /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> /// <exception cref="ArgumentException"></exception> public Mock <DbSet <T> > MockedSet <T>() where T : class { var type = typeof(T); EnsureEntityExistsAsSet(type); if (!DbSetProperties.ContainsKey(type)) { throw new ArgumentException($"Type {type.Name} is not in a DbSet in this context."); } if (!DbSetMocks.ContainsKey(type)) { MockDbSetForEntity(type); } return((Mock <DbSet <T> >)DbSetMocks[type]); }
private void MockDbSet(PropertyInfo dbSetProperty) { var dbSetType = dbSetProperty.PropertyType; var dbSetEntityType = dbSetType.GenericTypeArguments[0]; if (DbSetMocks.ContainsKey(dbSetEntityType)) { return; } var mockType = typeof(Mock <>); var mockSetType = mockType.MakeGenericType(dbSetType); var mockSet = Activator.CreateInstance(mockSetType); var mockSetData = DbSetCollections[dbSetEntityType]; var mockDataSetQueryable = mockSetData.AsQueryable(); SetupQueryableMethods(mockSetType, mockDataSetQueryable, mockSet, dbSetEntityType); CallMoqSetup(dbSetProperty, dbSetType, mockSet); DbSetMocks[dbSetEntityType] = mockSet; }