Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Unit testing

Unit testing

Scheduled Pinned Locked Moved C#
tutorialquestioncsharpvisual-studiocom
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tichaona J
    wrote on last edited by
    #1

    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);
                }                
            }
    
        }
    
    F D D 3 Replies Last reply
    0
    • T Tichaona J

      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);
                  }                
              }
      
          }
      
      F Offline
      F Offline
      fjdiewornncalwe
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • T Tichaona J

        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);
                    }                
                }
        
            }
        
        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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

        T P 2 Replies Last reply
        0
        • D 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 Kreskowiak

          T Offline
          T Offline
          Tichaona J
          wrote on last edited by
          #4

          Right 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...

          1 Reply Last reply
          0
          • D 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 Kreskowiak

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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

            My blog | My articles | MoXAML PowerToys | Onyx

            D 1 Reply Last reply
            0
            • P Pete OHanlon

              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

              My blog | My articles | MoXAML PowerToys | Onyx

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • T Tichaona J

                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);
                            }                
                        }
                
                    }
                
                D Offline
                D Offline
                David Ewen
                wrote on last edited by
                #7

                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);
                }

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups