dotMemory unit & test, newbie question

Answered

I've got a simple test that's made of

<pre>

[Test]
public void TestMethod2()
{
var mimmo = new Mimmo();

GC.WaitForPendingFinalizers();
GC.Collect();


dotMemory.Check(memory =>
{
Assert.That(memory.GetObjects(where => where.Type.Is<Mimmo>()).ObjectsCount, Is.EqualTo(0));
});
}

}

public class Mimmo
{
public int Id { get; set; }
}

</pre>

when I perform the test it fails saying there's one instance of Mimmo, shouldn't it be 0 since I've called the GC?

 

Thanks in advnance

 

1 comment

Hello,

It occurs because test is started in Debug mode. Local variables will not be collected until the method is completed. Please move code to separate method or run test in Release configuration:

  [Test]
  public void TestMethod2()
  {

    AllocateMemory();

    dotMemory.Check(memory =>
    {
      Assert.IsTrue(memory.GetObjects(where => where.Type.Is<Mimmo>()).ObjectsCount == 0);
    });
  }

  public static void AllocateMemory()
  {
    var mimmo = new Mimmo();

    GC.WaitForPendingFinalizers();
    GC.Collect();
  }

0

Please sign in to leave a comment.