Unit testing
-
Just watching this VSTS 2008 Unit Testing 1 of 1 lets you know how to test methods that return something, my question here is how do you test methods in Visual Studio 2008 that don't return any value? For example:
private void BtnLoad_Click(object sender, RoutedEventArgs e)
{
//Declare a new file dialog box
OpenFileDialog dlg = new OpenFileDialog();//Set properties for the dialog... dlg.Title = "Select one or more media files"; dlg.Multiselect = IsEnabled; dlg.Filter = "Media files(\*.mp3;\*.wav;\*.wma;\*.avi;\*.mp4;\*.mpg;\*.wmv)|\*.mp3;\*.wav;\*.wma;\*.avi;\*.mp4;\*.mpg;\*.wmv|All files(\*.\*)|\*.\*"; //The result of the open file dialog is either true or false (didn't work). Nullable<bool> result = dlg.ShowDialog(); //If the result of the open file dialog was true then.... if (result == true) { string\[\] files; files = dlg.FileNames; //Add each file into the list box foreach (string file in files) { lstBxList.Items.Add(file); } } }
-
Just watching this VSTS 2008 Unit Testing 1 of 1 lets you know how to test methods that return something, my question here is how do you test methods in Visual Studio 2008 that don't return any value? For example:
private void BtnLoad_Click(object sender, RoutedEventArgs e)
{
//Declare a new file dialog box
OpenFileDialog dlg = new OpenFileDialog();//Set properties for the dialog... dlg.Title = "Select one or more media files"; dlg.Multiselect = IsEnabled; dlg.Filter = "Media files(\*.mp3;\*.wav;\*.wma;\*.avi;\*.mp4;\*.mpg;\*.wmv)|\*.mp3;\*.wav;\*.wma;\*.avi;\*.mp4;\*.mpg;\*.wmv|All files(\*.\*)|\*.\*"; //The result of the open file dialog is either true or false (didn't work). Nullable<bool> result = dlg.ShowDialog(); //If the result of the open file dialog was true then.... if (result == true) { string\[\] files; files = dlg.FileNames; //Add each file into the list box foreach (string file in files) { lstBxList.Items.Add(file); } } }
You're trying to run an automated test on against a UI method. I may simply be a little old-school, but I typically don't write unit tests against UI methods because of the interaction required.
I wasn't, now I am, then I won't be anymore.
-
Just watching this VSTS 2008 Unit Testing 1 of 1 lets you know how to test methods that return something, my question here is how do you test methods in Visual Studio 2008 that don't return any value? For example:
private void BtnLoad_Click(object sender, RoutedEventArgs e)
{
//Declare a new file dialog box
OpenFileDialog dlg = new OpenFileDialog();//Set properties for the dialog... dlg.Title = "Select one or more media files"; dlg.Multiselect = IsEnabled; dlg.Filter = "Media files(\*.mp3;\*.wav;\*.wma;\*.avi;\*.mp4;\*.mpg;\*.wmv)|\*.mp3;\*.wav;\*.wma;\*.avi;\*.mp4;\*.mpg;\*.wmv|All files(\*.\*)|\*.\*"; //The result of the open file dialog is either true or false (didn't work). Nullable<bool> result = dlg.ShowDialog(); //If the result of the open file dialog was true then.... if (result == true) { string\[\] files; files = dlg.FileNames; //Add each file into the list box foreach (string file in files) { lstBxList.Items.Add(file); } } }
Your code is incorrectly written. This is a UI event handler and the code that you hav in it DOES return a value. This dialog box code should be in its own method that returns a fully qualified path to the caller. It should NOT be sitting inside an event handler. You also wouldn't run a unit test against a method that requires user interaction. Another question. Why on earth would you use a nullable boolean to get the return value of a dialog box? It's either going to return true or false, never null. So, what's the point?
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Your code is incorrectly written. This is a UI event handler and the code that you hav in it DOES return a value. This dialog box code should be in its own method that returns a fully qualified path to the caller. It should NOT be sitting inside an event handler. You also wouldn't run a unit test against a method that requires user interaction. Another question. Why on earth would you use a nullable boolean to get the return value of a dialog box? It's either going to return true or false, never null. So, what's the point?
A guide to posting questions on CodeProject[^]
Dave KreskowiakRight I see what your saying...First of all I should have pointed out that I am working with WPF hence use a nullable boolean to get the return value of a dialog box, the other way of calling a dialog box does not seem to work when developing a WPF based app. Secondly I used this as am example which I have to say after the response I got from fock was a bad example of which I was hoping fock would use it to illustrate how to test a method that does not return anything...Please bear in mind that I am a rooky,so if I say something that seems obvious to you, may not be the case with me...
-
Your code is incorrectly written. This is a UI event handler and the code that you hav in it DOES return a value. This dialog box code should be in its own method that returns a fully qualified path to the caller. It should NOT be sitting inside an event handler. You also wouldn't run a unit test against a method that requires user interaction. Another question. Why on earth would you use a nullable boolean to get the return value of a dialog box? It's either going to return true or false, never null. So, what's the point?
A guide to posting questions on CodeProject[^]
Dave KreskowiakDave Kreskowiak wrote:
Why on earth would you use a nullable boolean to get the return value of a dialog box? It's either going to return true or false, never null. So, what's the point?
Actually, it should return a DialogResult. The use of a bool condition would indicate that it's a WPF/SL dialog box, and this does return a nullable boolean condition.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
Dave Kreskowiak wrote:
Why on earth would you use a nullable boolean to get the return value of a dialog box? It's either going to return true or false, never null. So, what's the point?
Actually, it should return a DialogResult. The use of a bool condition would indicate that it's a WPF/SL dialog box, and this does return a nullable boolean condition.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
I haven't done any WPF until very recently, and nothing on dialogs yet, so my ignorance is showing through there. :-D
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Just watching this VSTS 2008 Unit Testing 1 of 1 lets you know how to test methods that return something, my question here is how do you test methods in Visual Studio 2008 that don't return any value? For example:
private void BtnLoad_Click(object sender, RoutedEventArgs e)
{
//Declare a new file dialog box
OpenFileDialog dlg = new OpenFileDialog();//Set properties for the dialog... dlg.Title = "Select one or more media files"; dlg.Multiselect = IsEnabled; dlg.Filter = "Media files(\*.mp3;\*.wav;\*.wma;\*.avi;\*.mp4;\*.mpg;\*.wmv)|\*.mp3;\*.wav;\*.wma;\*.avi;\*.mp4;\*.mpg;\*.wmv|All files(\*.\*)|\*.\*"; //The result of the open file dialog is either true or false (didn't work). Nullable<bool> result = dlg.ShowDialog(); //If the result of the open file dialog was true then.... if (result == true) { string\[\] files; files = dlg.FileNames; //Add each file into the list box foreach (string file in files) { lstBxList.Items.Add(file); } } }
As defined that method can not be unit tested as it requires user input (to select the file). But as you want an example on how to test a method that doesn't return a value it can be refactored slightly as below. To test a method that returns void you test for the change in system state that the method call is meant to impose. In this case the listbox should contain 2 items after the call, granted testing if items were added to a listbox is of dubious benefit but this is just for illustrative purposes.
private void BtnLoad_Click(object sender, RoutedEventArgs e)
{
//Declare a new file dialog box
OpenFileDialog dlg = new OpenFileDialog();//Set properties for the dialog... dlg.Title = "Select one or more media files"; dlg.Multiselect = IsEnabled; dlg.Filter = "Media files(\*.mp3;\*.wav;\*.wma;\*.avi;\*.mp4;\*.mpg;\*.wmv)|\*.mp3;\*.wav;\*.wma;\*.avi;\*.mp4;\*.mpg;\*.wmv|All files(\*.\*)|\*.\*"; //The result of the open file dialog is either true or false (didn't work). Nullable<bool> result = dlg.ShowDialog(); //If the result of the open file dialog was true then.... if (result == true) { LoadFilesToListbox(dlg.FileNames); }
}
public void LoadFilesToListbox(string[] files)
{
//Add each file into the list box
foreach (string file in files)
{
lstBxList.Items.Add(file);
}
}[TestMethod]
public void TestLoadFilesToListbox()
{
LoadFilesToListbox(new string[] { "file1.txt", file2.dat" });
Assert.AreEqual(2, lstBxList.Items.Count);
}