title: How to test possible memory leaks caused by references after the destruction of the tested object (DUnitX, Spring4D 1.2.2)
body: <p><code>TMyClass</code> contains two references. A reference to an <code>IList&lt;integer&gt;</code> and a reference to <code>IMyInterface</code>. The mocking of <code>IList&lt;integer&gt;</code> is not necessary. The framework probably well tested, well predictable behavior, so I could see it as a Data object. But <code>IMyInterface</code> is an untested service, so I should mock it in a unit test.
  I want to check for memory leaks so I want to test the modifications of the RefCount-s of the references after the subject destroyed.
  The 'RefCount' of the <code>IList&lt;integer&gt;</code> changes in the right way. But I can't say the same for the mocked <code>IMyInterface</code> (in my solution). How could I test that the references does not cause  memory leaks? Or a test like this is an integration test and should I test it always with real instances?</p>
  <pre><code>unit Unit1;
  
  
  interface
  
  uses
      DUnitX.TestFramework
    , Spring.Collections
    , Spring.Mocking
    ;
  
  type
  IMyInterface = interface ( IInvokable )
    ['{76B13784-6CCF-4A87-882C-E624F003B082}']
    procedure foo;
  end;
  
  TMyClass = class
    private
      fList : IList&lt;integer&gt;;
      fMyInterface : IMyInterface;
  
    public
      constructor Create( list_ : IList&lt;integer&gt;; myInterface_ : IMyInterface );
  
  end;
  
  [TestFixture]
  TMyClassTest = class
    protected
      function getInterfaceRefCount( const interface_ : IInterface ) : integer;
  
    public
      [Test]
      procedure listRefCountTest;
      [Test]
      procedure myInterfaceRefCountTest;
  
  end;
  
  implementation
  
  uses
    System.SysUtils
    ;
  
  constructor TMyClass.Create( list_ : IList&lt;integer&gt;; myInterface_ : IMyInterface );
  begin
    inherited Create;
    fList := list_;
    fMyInterface := myInterface_;
  end;
  
  function TMyClassTest.getInterfaceRefCount( const interface_ : IInterface ) : integer;
  begin
    result := ( interface_ as TInterfacedObject ).RefCount;
  end;
  
  procedure TMyClassTest.listRefCountTest;
  var
    list : IList&lt;integer&gt;;
    myInterfaceMock : Mock&lt;IMyInterface&gt;;
    myClass : TMyClass;
    listRefCount : integer;
  begin
    list := TCollections.CreateList&lt;integer&gt;;
    myClass := TMyClass.Create( list, myInterfaceMock );
    try
      listRefCount := getInterfaceRefCount( list );
    finally
      FreeAndNIL( myClass );
    end;
    Assert.AreEqual( listRefCount-1, getInterfaceRefCount( list ) );
  end;
  
  procedure TMyClassTest.myInterfaceRefCountTest;
  var
    list : IList&lt;integer&gt;;
    myInterfaceMock : Mock&lt;IMyInterface&gt;;
    myClass : TMyClass;
    myInterfaceRefCount : integer;
  begin
    list := TCollections.CreateList&lt;integer&gt;;
    myClass := TMyClass.Create( list, myInterfaceMock );
    try
      myInterfaceRefCount := getInterfaceRefCount( myInterfaceMock.Instance );
    finally
      FreeAndNIL( myClass );
    end;
    Assert.AreEqual( myInterfaceRefCount-1, getInterfaceRefCount( myInterfaceMock.Instance ) );
  end;
  
  initialization
    TDUnitX.RegisterTestFixture(TMyClassTest);
  
  end.
  </code></pre>