【发布时间】:2019-07-31 16:11:02
【问题描述】:
private 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
}
这些代码位于表单中的“保存”按钮下,我想在单元测试类中调用它们以测试它们。知道我该怎么做吗?谢谢
【问题讨论】:
-
将可测试的代码移动到您可以测试的类中,并从按钮单击事件中调用该类。无论如何,这是一个很好的做法,数据库代码永远不应该在那里。
标签: c# forms function unit-testing button