我怎么能写一个单元测试使用的WinForms的视图控制器类? [英] How can I write a unit test for a controller class that uses winforms for views?

查看:210
本文介绍了我怎么能写一个单元测试使用的WinForms的视图控制器类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人能够成功单元测试方法是,根据需要,加上对System.Windows.Forms.Form类?

Has anyone been able to successfully unit test methods that are, by necessity, coupled to the System.Windows.Forms.Form class?

我最近一直在一个C#WinForms应用程序,试图用一个MVC结构的构建。这是相当困难的,因为该框架没有真正建立考虑到这一点。

I've recently been working on a C# winforms application, trying to build it with an MVC structure. This is difficult enough, given that the framework isn't really built with this in mind.

然而,当你把单元测试到混合变得更加强硬。我一直在确保我的控制器没有连接到具体的视图类,这样我就可以使用存根/模拟进行单元测试。但引用Form类的地方是不可避免的,这些方法都需要进行测试。

However, it gets even tougher when you throw unit testing into the mix. I've been making sure that my controllers are not coupled to concrete view classes, so that I can use a stub/mock for unit testing. But referencing the Form class somewhere is unavoidable, and these methods do need to be tested.

我一直使用起订量的,因为它有一些很不错的类型安全特性,并允许嘲讽具体类型。但不幸的是,它并没有让我期望呼叫既不是虚拟的还是抽象的具体类型的方法或属性。而且,由于Form类不是建立与子类化于心,这是一个很大的问题。我需要能够从正在创建嘲笑Form类为prevent真正的窗口,通过预期的ShowDialog,例如。

I've been using Moq because it has some nice type-safety features, and allows mocking concrete types. But unfortunately, it doesn't allow me to "expect" calls to methods or properties on a concrete type that are neither virtual nor abstract. And since the Form class was not built with subclassing in mind, this is a big problem. I need to be able to mock the Form class to prevent real windows from being created, by "expecting" ShowDialog, for example.

所以我留下无法运行任何单元测试做很多互动形式的子类,而我的观点。

So I'm left unable to run any unit tests that do much interaction with subclasses of Form, which my views are.

有没有人在那里谁已经成功地进行单元测试这种类型的code?你是怎么做到的?

Is there anyone out there who has successfully unit tested this type of code? How did you do it?

这是一些其他的嘲讽框架可以解决?会被其他嘲讽框架串为基础的方法会受到同样的限制?我可以写我自己的明确的长手mock类,或将缺乏虚拟成员$ P $从能够晚饭preSS的窗口行为pvent我说的方法呢?

Is this something that other mocking frameworks can get around? Would the string-based methods used by other mocking frameworks be subject to the same constraints? Can I write my own explicit long-hand mock classes, or will the lack of virtual members prevent me from being able to suppress the window behavior that way too?

或者是有一些方法我都没有想到的构造我的课,这样的形式,加上code结束了在方法和类的琐碎复杂,这样我可以脱身没有明确的单元测试他们,没有我的良心打我了吧?

Or is there some way I haven't thought of to structure my classes so that the Forms-coupled code ends up in methods and classes of trivial complexity, such that I can get away without explicitly unit testing them, without my conscience beating me up for it?

推荐答案

我听过的最好的方法/用于单元测试与GUI元素是的谦虚对话模式/方法。从本质上说,窗体只是界面,所有的真正的工作是其他类中完成的。您单元测试提供的功能的类,然后就扎你的GUI事件,在这些类中相应的方法。

The best method I've heard of/used for unit testing with GUI elements is the Humble Dialog pattern/method. In essence, the Forms are just the interface, and all the real work is done in other classes. You unit test the classes that provide the functionality, and then just tie your GUI events to the appropriate methods in those classes.

这篇关于我怎么能写一个单元测试使用的WinForms的视图控制器类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆