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
- private void Moduletext_GotFocus(object sender, RoutedEventArgs e)
- {
- IEnumerable<JiffyUIFamiliarization.Common.Model.DataEntity.Module> module_list = parentWindow.createApp.GetModuleList();
- Popup pp = (Popup)module.Template.FindName("ScreenPopup", module);
- Label ll = (Label)module.Template.FindName("PopUpLabel", module);
- var targetTextBox = sender as TextBox;
- if (targetTextBox.Text == "Search..")
- {
- targetTextBox.Text = "";
- }
-
-
- var targetComboBox = this.module;
-
- if (targetTextBox == null) return;
-
- targetComboBox.Tag = "TextInput";
- targetComboBox.StaysOpenOnEdit = true;
- targetComboBox.IsEditable = true;
- targetComboBox.IsTextSearchEnabled = false;
-
- targetTextBox.TextChanged += (o, args) =>
- {
- var textBox = (TextBox)o;
-
- var searchText = textBox.Text;
-
- if (targetComboBox.Tag.ToString() == "Selection")
- {
- targetComboBox.Tag = "TextInput";
- targetComboBox.IsDropDownOpen = true;
- }
- else
- {
- if (targetComboBox.SelectionBoxItem != null)
- {
- targetComboBox.SelectedItem = null;
- targetTextBox.Text = searchText;
-
- targetTextBox.SelectionStart = targetTextBox.Text.Length;
- }
-
- if (string.IsNullOrEmpty(searchText))
- {
- targetComboBox.Items.Filter = item => true;
- targetComboBox.SelectedItem = default(object);
- }
- else
- targetComboBox.Items.Filter = item =>
- item.ToString().StartsWith(searchText, true, CultureInfo.InvariantCulture);
-
- var presentList = module_list.Where(item => item.GivenName.StartsWith(searchText, StringComparison.InvariantCultureIgnoreCase));
- if (presentList.Count() == 0)
- {
- ll.Content = $"Add {searchText}";
- pp.IsOpen = true;
- }
-
-
-
-
- targetTextBox.SelectionStart = targetTextBox.Text.Length;
- }
- };
- }
Please help me to write a unit test case for this or give me a code sample.