开发者

Testing WinForms user control - child control remains hidden

I am writing a WinForms user control in C# and want to test it (not TDD, I started by writing the component and making it work, but I kinda got tired of always finding bugs, so I want tests now). My problem is that I can't make a child control (a popup menu) become visible during testing. (I realize it won't show up - I don't want it to show up, but I still want to verify that it would show up when ran normally.)

Here's the relevant test code:

  updater.Raise(it => it.StateChanged += null, AutoUpdaterState.Ok);

[Note: the above is Moq code that will raise the event; it works, just ignore the details and think "event that will cause the menu to become visibl开发者_开发百科e is raised".]

  Assert.IsTrue(sut.checkToolStripMenuItem.Visible);

And here's the relevant code in the control:

      checkToolStripMenuItem.Visible = true;

The problem is... I put a breakpoint on this line, and Visible stays false after I run it. I understand it is probably because I'm testing it, and the menu doesn't actually become visible... which is ok, but how do I test this?


When testing the UI code, it is often much simpler if it is properly structured using some sort of MVC pattern, MVP for example for WinForms (if you are not doing so yet).

When you write these tests, you shouldn't even have WinForms referenced in your test project. I usually have a method on the view, for your example it would be ShowPopupMenu(). What I test is that under certain circumstances the presenter will call this method. I don't test the particular method, because I leave these simple things to UI, 99% of logic is in presenter anyway; and to show the popupmenu it takes one line of code...

However, if you don't have MVP, there are some libraries that can help you test WinForms, like NUnitForms.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜