lundi 16 mai 2016

How to unit test Dispose() using reflection?

I want to write unit tests for a class that implements IDisposable. The class has numerous private fields that also implement IDisposable. In my test, I want to verify that when I call Dispose(), it correctly calls Dispose() on all of its IDisposable fields. Essentially, I want my unit test to look like this:

var o = new ObjectUnderTest();
o.Dispose();
Assert.IsFalse(ObjectHasUndisposedDisposables(o));

I was thinking of using reflection to achieve this. It seems like this would be a fairly common requirement, but I can't find any examples of it.

Anyone tried this?





Aucun commentaire:

Enregistrer un commentaire