TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
sonu krishna
NA
35
1.1k
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
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 targetTextBox = sender as TextBox;
// var targetTextBox = targetComboBox?.Template.FindName("PART_EditableTextBox", targetComboBox) as TextBox;
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;
//textBox.CaretIndex = MaxValue;
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
;
}
//Keyboard.ClearFocus();
//Keyboard.Focus(targetTextBox);
//targetComboBox.IsDropDownOpen = true;
targetTextBox.SelectionStart = targetTextBox.Text.Length;
}
};
}
Please help me to write a unit test case for this or give me a code sample.
Reply
Answers (
0
)
TD Ameritrade API Assistance
transposing data table