Ich habe eine DbContext
mit mehreren DbSet<>
Eigenschaften. Einige dieser DbSet<>
haben Icollection<>
Objekte (Navigationseigenschaften).Wie zu überprüfen, dass alle Sammlungen in allen Entitäten im Kontext instanziiert werden
Ich möchte einen Komponententest schreiben, der prüft, ob all diese ICollection<>
Eigenschaften in meinen Entitätskonstruktoren instanziiert sind.
Hier ist, wo ich bin so weit:
// 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.
}
}
}
Von dort ich blockiert bin. Ich sehe nicht, wie ich testen kann, ob die Sammlung instanziert ist.
Danke für Ihre Hilfe!