如何测试SafeHandle?

时间:2016-07-17 19:21:17

标签: c# unit-testing native-code

我有以下课程:

 internal class House
 {
  public SafeHouse SafeHouseHandler { get; private set; }

   internal House()
    {
        IntPtr nativP = NativeMethods.UnitType();
        SafeHouseHandler = new SafeHouse(nativP);
    }
}

    public class SafeHouse : SafeHandleZeroOrMinusOneIsInvalid
    {
     public SafeHouse(IntPtr ownsHandle)
        : base(true)
    {
        SetHandle(ownsHandle);
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    protected override bool ReleaseHandle()
    {
        NativeMeth.Delete(handle);
        return true;
    }
  }

我想编写一个测试来确保调用ReleaseHandle

测试:

    [TestMethod]
    public void SafeHouseTest()
    {
        var house = new House();
        SafeHouse safeHouse = house.SafeHouseHandler;

        Assert.AreNotEqual(IntPtr.Zero, safeHouse.DangerousGetHandle());

        house = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();

        Assert.AreEqual(IntPtr.Zero, safeHouse.DangerousGetHandle());
    }

结果:

Assert.AreEqual failed. Expected:<0>. Actual:<489437772>. 

safeHouse为什么0house = null时没有返回$(function () { $('.TEST').click(function () { console.log($('.TEST').index(this)); }); });? 如果我的测试方法是错误的,那么任何测试此类课程的建议都将受到赞赏。

0 个答案:

没有答案