Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit testing method that uses UI controls
    text
    copied!<p>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. </p> <p>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:</p> <p>Say I have this method which takes a control and if it is a textbox it will clear the text property like this:</p> <pre><code> public static void clearall(this Control control) { if (control.GetType() == typeof(TextBox)) { ((TextBox)control).Clear(); } } </code></pre> <p>Then I want to test this method so I do something like this:</p> <pre><code> [TestMethod] public void TestClear() { List&lt;Control&gt; listofcontrols = new List&lt;Control&gt;(); 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); } } </code></pre> <p>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? </p> <p>NOTE: The above code is only an example, I didn't compile or run it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload