使用UI控件单元测试方法 [英] Unit testing method that uses UI controls

查看:185
本文介绍了使用UI控件单元测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一些方法,做表格的一些基本操作控件如文本框,组框,这些操作是通用的,可以在任何应用程序中使用。

I'm currently writing some methods that do some basic operations on form controls eg Textbox, Groupbox, these operations are generic and can be used in any application.

我开始写一些单元测试,只是想知道我应该使用System.Windows.Forms的找到了真正的表单控件,或者我应该只是小样的部分,我M试图测试。因此,例如:

I started to write some unit tests and was just wondering should I use the real form controls found in System.Windows.Forms or should I just mock up the sections that I'm trying to test. So for example:

说我有这种方法,需要控制,如果它是一个文本框将清除Text属性是这样的:

Say I have this method which takes a control and if it is a textbox it will clear the text property like this:

        public static void clearall(this Control control)
        {
            if (control.GetType() == typeof(TextBox))
            {
                ((TextBox)control).Clear();
            }
        }



然后我要测试这种方法让我做了什么像这样的:

Then I want to test this method so I do something like this:

        [TestMethod]
        public void TestClear() 
        {
            List<Control> listofcontrols = new List<Control>();
            TextBox textbox1 = new TextBox() {Text = "Hello World" };
            TextBox textbox2 = new TextBox() { Text = "Hello World" };
            TextBox textbox3 = new TextBox() { Text = "Hello World" };
            TextBox textbox4 = new TextBox() { Text = "Hello World" };

            listofcontrols.Add(textbox1);
            listofcontrols.Add(textbox2);
            listofcontrols.Add(textbox3);
            listofcontrols.Add(textbox4);

            foreach (Control control in listofcontrols)
            {
                control.clearall();
                Assert.AreEqual("", control.Text);
            }
        }



我应该加入到全球化志愿服务青年System.Window。形式到我的单元测试和使用真正的文本框对象吗?还是我做错了?

Should I be adding a referance to System.Window.Forms to my unit test and use the real Textbox object? or am I doing it wrong?

请注意:上面的代码只是一个例子,我没有编译或运行它

NOTE: The above code is only an example, I didn't compile or run it.

推荐答案

如果你想单元与UI控件模拟互动测试应用程序的逻辑,你应该做使用的 MVC模式。然后,你可以有一个存根看法,并呼吁从你的单元测试的控制器方法。

If you're trying to unit test the application logic by simulating interaction with the UI controls, you should do some abstraction using the MVC pattern. Then you can just have a stub view and call the controller methods from your unit tests.

如果这是你想要的单元测试的实际控制,你有我的。

If it's the actual controls you're trying to unit test, you've got me.

这篇关于使用UI控件单元测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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