Here is a part of a code of a simple WPF application: 3 textboxes, a dropdownList and a button. By clicking a button, there will be checking of input values.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (textBox1.Text.Length>127)
throw new ArgumentException();
if (string.IsNullOrEmpty(textBox2.Text))
errorsList.Add("You must to fill out textbox2");
else if (string.IsNullOrEmpty(textBox3.Text))
errorsList.Add("You must to fill out textbox3");
else if
{
Regex regex = new Regex(@".........");
Match match = regex.Match(emailTxt.Text);
if (!match.Success)
errorsList.Add("e-mail is inlvalid");
}
//.....
}
I have to test it by using any Unit testing Framework. I wonder is it possible to do Unit testing here? I guess it is not, right?
It is not possible to unit test the current code you have without refactoring. You should encapsulate that logic in a ViewModel class. I guess you can have something like
and
error/errorList/exListasObservableCollectionsinto the viev model too.With these precondition you can write a suite of unit tests checking your code behavior.