vendredi 31 mai 2019

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(); // executeing 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

Aucun commentaire:

Enregistrer un commentaire