简体   繁体   English

我应该对这节课进行单元测试吗?

[英]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; 如果只是{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. 众所周知,GUI代码很难进行单元测试。 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. 只需将您的业务逻辑放在GUI之外,即可轻松进行测试。 I generally test the gui code with with either manual or automated integration/acceptance tests. 我通常使用手动或自动集成/验收测试来测试gui代码。 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. 如果以后有人更改了代码或向其中添加了一些新代码,则您至少要进行一次测试,以确保该类能够按预期运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元测试-我如何测试该课程 - Unit Testing - How do I test this class C# 单元测试 - 您是否应该对基类中处理的派生类中的某些内容进行单元测试? - C# Unit Testing - Should you unit test something in a derived class that is taken care of in a base class? 我应该对我的引导程序进行单元测试,如果是这样的话? - Should I be unit testing my bootstrapper and if so how? ASP.NET MVC 4单元测试(我应该测试什么) - ASP.NET MVC 4 Unit testing (what i should test) 单元测试 - 我应该分开测试还是进行一次测试? - Unit testing - should I split up tests or have a single test? 如何(我应该)模拟 DocumentClient 以进行 DocumentDb 单元测试? - How to (should I) mock DocumentClient for DocumentDb unit testing? 单元测试时应该使用模拟对象吗? - Should I be using mock objects when unit testing? 单元测试时,我应该在ExpectedExceptionAttribute中使用哪种类型的异常? - Which type of exception should I use in ExpectedExceptionAttribute while unit testing ? 我应该使用回调还是引入公共字段来辅助单元测试? - Should I use a callback or introduce a public field to aid with unit testing? 我应该如何在Controller中模拟SignalR HubContext以进行单元测试? - How should I mock SignalR HubContext in Controller for Unit Testing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM