I have a DbContext
with multiple DbSet<>
properties. Some of those DbSet<>
have Icollection<>
objects (navigation properties).
I want to write a unit test which checks if all those ICollection<>
properties are instanced in my entities constructors.
Here is where I am this far:
// this.ctx is the instance of my DbContext
var allProps = this.ctx.GetType().GetProperties();
var allDbSets = allProps.Where(pi => pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>));
var allEntities = allDbSets.SelectMany(pi => pi.PropertyType.GetGenericArguments());
foreach (var entity in allEntities)
{
// get collections of entity
var genericProps = entity.GetProperties().Where(pi => pi.PropertyType.IsGenericType);
var collections = genericProps.Where(pi => pi.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>));
if(collections.Any())
{
foreach (var coll in collections)
{
// coll is the PropertyInfo of a ICollection<> object.
}
}
}
From there I'm blocked. I don't see how I can test that the collection is instanced.
Thanks for your help!
Aucun commentaire:
Enregistrer un commentaire