How do i do a unit test on a button click

Hi all I am trying to do unit tests on a button click event. and i cant figure it out for the life of me.
///A test for button1_Click (object, EventArgs)
        ///</summary>
        [DeploymentItem("Hotel Rate Calculator.exe")]
        [TestMethod()]
        public void button1_ClickTest()
        {
            Form1 target = new Form1();
 
            TestProject2.Hotel_Rate_Calculator_Form1Accessor accessor = new TestProject2.Hotel_Rate_Calculator_Form1Accessor(target);
 
            object sender = null; // TODO: Initialize to an appropriate value
 
            EventArgs e = null; // TODO: Initialize to an appropriate value
 
            accessor.button1_Click(sender, e);
 
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }
 
    }

Open in new window

HawaiiDragonAsked:
Who is Participating?
 
DabasConnect With a Mentor Commented:
Hello HawaiiDragon,

It does not make sense to unit test UI functionality.
It makes sense to unit test the code that the UI calls.

Hence, if your button1_Click code looks like this:

private void button1_Click()
{
 //Your code to calculate rates
}

Consider moving the calculation section to a method:

private void button1_Click()
{
      CalculateRates();
}

public void CalledMethod()
{
//Your code to calculate rates
}

Now you can call CalledMethod() from your unit test, and compare what you expect to calculate with the actual results.

Regards,

Dabas
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.