Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 570
  • Last Modified:

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

0
HawaiiDragon
Asked:
HawaiiDragon
1 Solution
 
DabasCommented:
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now