How can I unit test EventHandler function?

Weanich Sanchol 21 Reputation points
2021-08-31T04:31:28.03+00:00

according to example in https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.mousebuttoneventargs?view=net-5.0
how can I unit test this function

  private void MouseButtonDownHandler(object sender, MouseButtonEventArgs e)  
  {  
      Control src = e.Source as Control;  

      if (src != null)  
      {  
          switch (e.ChangedButton)  
          {  
              case MouseButton.Left:  
                  src.Background = Brushes.Green;  
                  break;  
              case MouseButton.Middle:  
                  src.Background = Brushes.Red;  
                  break;  
              case MouseButton.Right:  
                  src.Background = Brushes.Yellow;  
                  break;  
              case MouseButton.XButton1:  
                  src.Background = Brushes.Brown;  
                  break;  
              case MouseButton.XButton2:  
                  src.Background = Brushes.Purple;  
                  break;  
              default:  
                  break;  
          }  
      }  
  }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,249 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
329 questions
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2021-08-31T13:49:15.22+00:00

    To test the logic, maybe move the logic to separate functions or classes or projects and test them. For example:

    private void MouseButtonEventArgs( object sender, MouseButtonEventArgs e )
    {
        Control src = e.Source as Control;
        if( src != null )
        {
            var b = MyLogic.GetBrush( e.ChangedButton );
            if( b != null ) src.Background = b;
        }
    }
    
    . . .
    public class MyLogic
    {
        public static Brush GetBrush( MouseButton changedButton )
        {
            switch( changedButton )
            {
            case MouseButton.Left:
                return Brushes.Green;
            case MouseButton.Middle:
                return Brushes.Red;
            case MouseButton.Right:
                return Brushes.Yellow;
            case MouseButton.XButton1:
                return Brushes.Brown;
            case MouseButton.XButton2:
                return Brushes.Purple;
            default:
                return null;
            }
        }
    }
    . . .
    [TestClass]
    public class MyTest
    {
        [TestMethod]
        public void MyTest1( )
        {
            Assert.AreEqual( Brushes.Brown, MyLogic.GetBrush( MouseButton.XButton1 ) );
            // etc.
            // . . .
    
        }
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful