本帖最後由 luckiejacky 於 2015-4-29 14:19 編輯
How can I make this class Unit-Testable?
THX
- namespace RCS.followupperson
- {
- public partial class CompleteTask : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void FormView1_PreRender(object sender, EventArgs e)
- {
- if (FormView1.CurrentMode == FormViewMode.Insert)
- {
- Label ItemNo = (Label)FormView1.FindControl("ItemNoLabel1");
- ItemNo.Text = Request["ItemNo"].ToString();
- Int32 itemNo = Int32.Parse(ItemNo.Text);
- // Fill the last revised date information into the formview
- DateTime OriginalTargetDate = ReadOriginalTargetDateInfo(itemNo);
- TextBox Ori_Target_Finish_Date = (TextBox)FormView1.FindControl("Ori_Target_Finish_DateTextBox");
- Ori_Target_Finish_Date.Text = OriginalTargetDate.ToString("dd-MMM-yyyy");
- }
- }
- protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
- {
- using (var scope = new TransactionScope())
- {
- Int32 newAppID = -1;
- Label itemNoLabel = (Label)FormView1.FindControl("ItemNoLabel1");
- TextBox txtFinishDate = (TextBox)FormView1.FindControl("Finish_DateTextBox");
- Int32 ItemNo = Convert.ToInt32(itemNoLabel.Text);
- DateTime FinishDate = DateTime.Parse(txtFinishDate.Text);
- TextBox reasonTextBox = (TextBox)FormView1.FindControl("ReasonTextBox");
- String FinishReason = reasonTextBox.Text;
- Approvals app = new Approvals();
- app.ItemNo = ItemNo;
- app.AppType = ApprovalType.FINISH_JOB;
- app.CreatedBy = new Guid(Session["userId"].ToString());
- app.ProposedPersonnelID = new Guid(Session["userId"].ToString());
- app.ProposedDate = DateTime.Parse(txtFinishDate.Text);
- app.ApprovePersonID = Guid.Empty;
- app.ApprovalDate = (DateTime?)null;
- app.ApprovalReason = "";
- // 1) Insert Finish Job Approval for approved tracking
- Helpers.InsertApprovalsWithReturn(app, ref newAppID);
- // 2) Insert Finish Job Log for awaiting approval tracking
- InsertFinishTargetLog(newAppID, ItemNo, FinishDate, FinishReason);
- String FollowupPersonEmail = "";
- String SupervisorEmail = "";
- Helpers.GetEmailAddresses(new Guid(Session["userId"].ToString()), ref FollowupPersonEmail, ref SupervisorEmail);
- // 3) Send an Email notification to the followup person
- Helpers.SendingMail(FollowupPersonEmail, SupervisorEmail, "Finish Job", "Finish Job");
- scope.Complete();
- }
- Response.Redirect("~/followupperson/ViewIssue.aspx");
- }
- private DateTime ReadOriginalTargetDateInfo(Int32 ItemNo)
- {
- SqlConnection con = new SqlConnection();
- con.ConnectionString = ConfigurationManager.ConnectionStrings["RCS_2_02ConnectionString"].ConnectionString;
- // Read the latest revised target finish date
- SqlCommand comm = new SqlCommand(
- @"SELECT i.[ItemNo],
- max(i.[OriTargetFinishDate]) AS OriTargetFinishDate
- FROM [IssueLog] AS i
- WHERE
- (i.[ItemNo] = @ItemNo)
- group by i.[ItemNo]
- ", con);
- con.Open();
- comm.Parameters.AddWithValue("@ItemNo", ItemNo);
- SqlDataReader rdr = comm.ExecuteReader();
- if (rdr.HasRows)
- {
- rdr.Read();
- DateTime OriTargetFinishDate = DateTime.Parse(rdr["OriTargetFinishDate"].ToString());
- rdr.Close();
- con.Close();
- return OriTargetFinishDate;
-
- }
- con.Close();
- return DateTime.MinValue;
-
- }
-
- protected int InsertFinishTargetLog(Int32 newAppID, Int32 ItemNo, DateTime FinishDate, String FinishReason)
- {
- String ComText = @"INSERT INTO FinishTargetLog
- (AppID, ItemNo, FinishDate, FinishReason)
- VALUES (@AppID, @ItemNo, @FinishDate, @FinishReason)";
- try
- {
- using (var con = new SqlConnection())
- {
- con.ConnectionString = ConfigurationManager.ConnectionStrings
- ["RCS_2_02ConnectionString"].ConnectionString;
- con.Open();
- {
- using (var comm = new SqlCommand(ComText, con))
- {
- comm.Parameters.AddWithValue("@AppID", newAppID);
- comm.Parameters.AddWithValue("@ItemNo", ItemNo);
- comm.Parameters.AddWithValue("@FinishDate", FinishDate.ToShortDateString());
- comm.Parameters.AddWithValue("@FinishReason", FinishReason);
- int res = comm.ExecuteNonQuery();
- con.Close();
- return 0;
- }
- }
- }
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
複製代碼 |