Belirli bir derleme için boş kurucularla tüm soyut sınıfların, örneklendiğinde koleksiyon özelliklerini başlatacağını kontrol edecek bir birim sınaması yapmaya çalışıyorum. Where
fıkra benim diğer montaj doğru özelliklerini kapmak olmadığından Sınıfların tüm toplama özelliklerinin başlatılacağını test etmek için yansıma kullanıyorum
[TestFixture]
public class DtoTests
{
[Test]
public void NamespaceDtos_WhenDefaultConstructorIsCalled_InstantiatesCollectionProperties()
{
bool testWasMeaningful = false;
foreach (var type in GetEntityTypesWithDefaultConstructor())
{
var instance = Activator.CreateInstance(type);
var collectionProps = type
.GetProperties()
.Where(p => typeof(ICollection<>).IsAssignableFrom(p.PropertyType));
foreach (var prop in collectionProps)
{
var val = prop.GetValue(instance);
Assert.That(val, Is.Not.Null.And.Empty, string.Format("[{0}.{1}]", type.Name, prop.Name));
testWasMeaningful = true;
}
}
Assert.That(testWasMeaningful, "Expected at least one assertion.");
}
private IEnumerable<Type> GetEntityTypesWithDefaultConstructor()
{
return Assembly
.GetAssembly(typeof(MyProject.Dto.Things.Item))
.GetTypes()
.Where(t => !t.IsAbstract)
.Where(t => t.GetConstructor(Type.EmptyTypes) != null);
}
}
Ancak bu işe yaramazsa,:
namespace MyProject.Dto.Things
{
public class Item
{
public Item()
{
// Second batch of props should be instantiated here...
}
// Properties that my test doesn't/shouldn't care about:
public int IntProp { get; set; }
public string StringProp { get; set; }
// Properties my test should account for:
public List<string> ListProp { get; set; }
public IList<string> IListProp { get; set; }
public ISet<string> ISetProp { get; set; }
public ICollection<string> ICollectionProp { get; set; }
public IDictionary<string, string> IDictionaryProp { get; set; }
public Stack<string> StackProp { get; set; }
public string[] ArrayProp { get; set; }
}
}
Benim ilk girişimi whas bu: İşte test altında sistem. Testim başarısız çünkü tek bir özelliği test etmiyor.
var collectionProps = type
.GetProperties()
.Where(m => m.PropertyType.IsGenericType && m.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>));
Ama bu sadece Item.ICollectionProp
yakalar ve diğerlerini test başarısız: Girişiminde
böyle this answer denedim hiçbir 1
düzeltin.
böyle hiçbir 2
İkincisi, ben (kelimesi kelimesine GetICollectionOrICollectionOfTProperties
yöntem reklamı kullanarak) this answer to another question denedim teşebbüs düzeltme: ironik
var collectionProps = type
.GetICollectionOrICollectionOfTProperties();
Ama yanlış geçecek Test, Item.ICollectionProp
'un başlatılmamış olsa bile. ,
var collectionProps = type
.GetProperties()
.Where(p => typeof(IEnumerable).IsAssignableFrom(p.PropertyType));
Ama string
IEnumerable
tutar olduğundan, Item.StringProp
üzerinde başarısız olur: Girişiminde
böyle, sadece tüm IEnumerable
özellikler hiçbir 3
Ben de denedim test düzeltmek ve bunu burada test etmek istemiyorum.
Ben sadece çalışmak için bir çözüm kazanılmış vardı olsaydın ben bile, sorum şu birinin yinelenen olduğunu düşünmek istiyorum özellikle nr 2. ile yanlış nereye emin değilim.
Alt satır: (X) Sınıf oluşturulduğunda, boş yapıcılarla sınıfların tüm toplama özelliklerinin örneklendirildiğini nasıl test edebilirim ve/veya (Y) Yansımayı kullanarak bir türün tüm koleksiyon özelliklerini nasıl bulabilirim?