Search code examples
c#winformsunit-testingmstestmessagebox

How do I test did a message box shows up or not in unit testing?


I wanted to test, will the message box show up when my code request it. But I am not so sure what to assert or am I even doing the right thing.

public void btnSave_Click(object sender, EventArgs e)
        {
            if(txtFirstName.Text.Trim() != "" && txtLastName.Text.Trim() != "" && txtContact.Text.Trim() != "")
            {
                Regex reg = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); // Only accepting proper email
                Match match = reg.Match(txtEmail.Text.Trim());
                if (match.Success)
                { using (SqlConnection sqlCon = new SqlConnection(connectionString)) // Connecting info to database
                    {
                        sqlCon.Open();
                        SqlCommand sqlCmd = new SqlCommand("ContactAddorEdit", sqlCon);
                        sqlCmd.CommandType = CommandType.StoredProcedure;
                        sqlCmd.Parameters.AddWithValue("@PhoneBookID", PhoneBookID); //connecting each value to database
                        sqlCmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text.Trim());
                        sqlCmd.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim());
                        sqlCmd.Parameters.AddWithValue("@Contact", txtContact.Text.Trim());
                        sqlCmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
                        sqlCmd.Parameters.AddWithValue("@Address", txtAddress.Text.Trim());
                        sqlCmd.ExecuteNonQuery(); // Executing the query in database
                        MessageBox.Show("Submitted successfully"); // Showing message when success
                        Clear(); // Clearing the form
                        GridFill();// Refreshing the table
                    }
                }
                else
                {
                    MessageBox.Show(" Please enter a valid Email"); // Showing message when email is not valid
                }
            }
            else
            {
                MessageBox.Show("Please fill Mandatory fields"); // If no input this message will show
            }

        }

So if the text box got a empty string there will be an message box pops up and say "Please fill Mandatory fields"

and here is the test that I am trying write

        [TestMethod]
        public void TestMethod1()
        {
            Form1 form1 = new Form1();
            form1.txtFirstName.Text = "";
            Assert.IsTrue(MessageBox.Show("Please fill Mandatory fields") ;
        }

What kind of assert should and use and how do I write it? Can I test it like this? Thank you


Solution

  • You don't use unit tests to test the user interface. They are for testing the logic. A unit test can't click the buttons (who or what will click OK when your message box pops up so the test can finish?) there are UI testing frameworks that can.

    Think about what you are trying to test here. Test1 is testing that the form will correctly check that the mandatory fields are filled in. Create a method for that logic, and unit test it:

    public bool MandatoryFieldsArePopulated()
    {
         return txtFirstName.Text.Trim() != "" 
             && txtLastName.Text.Trim() != "" 
             && txtContact.Text.Trim() != "";
    }
    

    this can easily be tested with unit tests. Then the first line of the btnSave_Click method will be

    if (MandatoryFieldsArePopulated())
    

    Structuring your code like this not only enables you to test the logic units individually, it also makes the code clearer and easier to read.