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.
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:
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);
}
}
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.