sonu krishna

sonu krishna

  • 2k
  • 2
  • 181

UnitTesting for WPF events

Sep 16 2020 2:28 AM
Hi All,
I am kind of new to C# and WPF, even though I am on my way to create a web scratching application using WPF. I am almost finished with my code and its working good. Now I want to write a set of unit test cases for this particular project, I started using MSTest. But when coming to events and big methods I am struggling to write unit tests for the same. I am adding a code snippet from my code below, can anyone check and give an idea about how to write a unit test case for that particular part. It is an autocomplete text box, where based on the input it will filter and I wrote it inside a Get focus event. adding the code below
 
  1. private void Moduletext_GotFocus(object sender, RoutedEventArgs e)  
  2.         {  
  3.             IEnumerable<JiffyUIFamiliarization.Common.Model.DataEntity.Module> module_list = parentWindow.createApp.GetModuleList();  
  4.             Popup pp = (Popup)module.Template.FindName("ScreenPopup", module);  
  5.             Label ll = (Label)module.Template.FindName("PopUpLabel", module);  
  6.             var targetTextBox = sender as TextBox;  
  7.             if (targetTextBox.Text == "Search..")  
  8.             {  
  9.                 targetTextBox.Text = "";  
  10.             }  
  11.             //var targetTextBox = sender as TextBox;  
  12.             // var targetTextBox = targetComboBox?.Template.FindName("PART_EditableTextBox", targetComboBox) as TextBox;  
  13.             var targetComboBox = this.module;  
  14.   
  15.             if (targetTextBox == null) return;  
  16.   
  17.             targetComboBox.Tag = "TextInput";  
  18.             targetComboBox.StaysOpenOnEdit = true;  
  19.             targetComboBox.IsEditable = true;  
  20.             targetComboBox.IsTextSearchEnabled = false;  
  21.   
  22.             targetTextBox.TextChanged += (o, args) =>  
  23.             {  
  24.                 var textBox = (TextBox)o;  
  25.   
  26.                 var searchText = textBox.Text;  
  27.   
  28.                 if (targetComboBox.Tag.ToString() == "Selection")  
  29.                 {  
  30.                     targetComboBox.Tag = "TextInput";  
  31.                     targetComboBox.IsDropDownOpen = true;  
  32.                 }  
  33.                 else  
  34.                 {  
  35.                     if (targetComboBox.SelectionBoxItem != null)  
  36.                     {  
  37.                         targetComboBox.SelectedItem = null;  
  38.                         targetTextBox.Text = searchText;  
  39.                         //textBox.CaretIndex = MaxValue;  
  40.                         targetTextBox.SelectionStart = targetTextBox.Text.Length;  
  41.                     }  
  42.   
  43.                     if (string.IsNullOrEmpty(searchText))  
  44.                     {  
  45.                         targetComboBox.Items.Filter = item => true;  
  46.                         targetComboBox.SelectedItem = default(object);  
  47.                     }  
  48.                     else  
  49.                         targetComboBox.Items.Filter = item =>  
  50.                                 item.ToString().StartsWith(searchText, true, CultureInfo.InvariantCulture);  
  51.   
  52.                     var presentList = module_list.Where(item => item.GivenName.StartsWith(searchText, StringComparison.InvariantCultureIgnoreCase));  
  53.                     if (presentList.Count() == 0)  
  54.                     {  
  55.                         ll.Content = $"Add {searchText}";  
  56.                         pp.IsOpen = true;  
  57.                     }  
  58.                     
  59.                     //Keyboard.ClearFocus();  
  60.                     //Keyboard.Focus(targetTextBox);  
  61.                     //targetComboBox.IsDropDownOpen = true;  
  62.                     targetTextBox.SelectionStart = targetTextBox.Text.Length;  
  63.                 }  
  64.             };  
  65.         }  
 Please help me to write a unit test case for this or give me a code sample.

Brought to you by: