I just found this gem in a post on another subject on Krzysztof Koźmic’s blog and thought it merited its own post. Beautifully simply solution for asserting that a class doesn’t hold a reference to an object that it should not.
[Fact] public void Freezable_should_not_hold_any_reference_to_created_objects() { var pet = Freezable.MakeFreezable<Pet>(); var petWeakReference = new WeakReference(pet, false); pet = null; GC.Collect(); Assert.False(petWeakReference.IsAlive, "Object should have been collected"); } |