Currently, I am writing several methods that perform some basic operations with form controls, for example Textbox, Groupbox, these operations are common and can be used in any application.
I started writing some unit tests and just wondered if I should use real form controls located in System.Windows.Forms, or should I just mock up the sections I'm trying to test. For example:
Let's say I have this method that takes control, and if it is a text field, it will clear the text property as follows:
public static void clearall(this Control control) { if (control.GetType() == typeof(TextBox)) { ((TextBox)control).Clear(); } }
Then I want to test this method so that 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 add a link to System.Window.Forms to my unit test and use a real Textbox object? or am i doing it wrong?
NOTE. The above code is just an example; I did not compile and run it.
c # unit-testing mocking
Nathan w
source share