public void StaticEventTest() { WeakReference weakReference = null; // Run this in a delegate so that the local variable gets garbage collected (if that's possible) new Action(() => { var sut = new ShortLiving(); sut.SubscribeToStaticEvent(); weakReference = new WeakReference(sut); })(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); weakReference.Target.ShouldNotBeNull(); }
public void WeakEventTest() { WeakReference weakReference = null; var longLiving = new LongLivingWithWeakEvent(); // Run this in a delegate so that the local variable gets garbage collected (if that's possible) new Action(() => { var sut = new ShortLiving(longLiving); weakReference = new WeakReference(sut); })(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); weakReference.Target.ShouldBeNull(); }