简体   繁体   中英

Should I be unit testing this class?

I know this maybe a very basic question but I'm having a bit of a mind blank at the moment. Should I be unit testing this class.

public class MapinfoWindowHandle : IWin32Window
    {
        IntPtr handle;

        public MapinfoWindowHandle(IntPtr mapinfoHandle)
        {
            this.handle = mapinfoHandle;     
        }

        #region IWin32Window Members

        IntPtr IWin32Window.Handle
        {
            get { return this.handle; }
        }

        #endregion
    }

If I should be what should I be testing for?
I use it like this:

IntPtr handle = new IntPtr(100);
myform.show(new MapinfoWindowHandle(handle));

The only thing that I can see is making sure you get out the handle that you put in via your constructor. I know that it's obvious that you implemented it this way, but a test would assure you that it stays this way. I would test this only because you are injecting it via the constructor. If it was just { get; set; } I probably wouldn't.

 [TestMethod]
 public void ConstructorTest()
 {
      IntPtr handle = new IntPtr(100);
      MapinfoWindowHandle winHandle = new MapinfoWindowHandle(handle);
      Assert.AreEqual( handle, ((IWin32Window)winHandle).Handle );
 }

我肯定会尝试尝试使用NULL(0)或INVALID_HANDLE_VALUE(-1)构造它,并且如果合适的话,可能会将它同时放在两者中(尚不清楚是否可以使用IntPtr.Zero初始化该类,但是几乎可以肯定-1是无效的。

Yes. If a class is worth writing, it is worth testing

The pragmatist in me says no, because the class does "nothing", so there is "nothing" to test. But sure you could still test it, just for documentation purposes, and as a contract for future developers.

GUI code is notoriously hard to unit test. I've never found it to be too useful in my own applications. Just keep your business logic out of your GUI so you can easily test that. I generally test the gui code with with either manual or automated integration/acceptance tests. It's hard to write a test to verify that something "looks right."

Yes write the test. If later someone changes the code or adds some new code to it, you have at least one test to make sure the class works as it was intended to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM