I have an object, that have internal logic related to events subscription.
I want to write an autotest, that checks if the object can be collected by GC after certain steps.
For example:
public interface IEventPublisher {
event Action Evt;
}
public class MyClass : IDisposable {
private IEventPubliser _evtPub;
public MyClass(IEventPublisher evtPub) {
_evtPub = evtPub;
}
public void Initialize() {
_evtPub.Evt += EvtHandler();
}
private void EvtHandler() {
// *some logic*
}
public void Dispose() {
_evtPub.Evt -= EvtHandler();
}
}
[Test]
public void MemoryLeakTest() {
{
// Arrange
var evtPub = // *mock event publisher with NSubstitute or something*
var myObj = new MyClass(evtPub);
// Act
myObj.Initialize();
// *do some staff maybe*
myObj.Dispose();
}
// Assert
// ASSERT_NO_REFERENCES_TO_myObj
}
So, the main question is what should I put instead of ASSERT_NO_REFERENCES_TO_myObj
? And is it at least possible?