作者: luckiejacky 時間: 2015-4-28 15:19 標題: 怎樣在Visual Studio Community 2013 加 Unit Test Project?
Hi
因在Project Startup 時無加Unit Testing Module
唔知點可以後加番落去...
THX
[attach]1786145[/attach]
作者: l0001 時間: 2015-4-28 15:22
提示: 作者被禁止或刪除 內容自動屏蔽
作者: luckiejacky 時間: 2015-4-28 15:44
Thanks, I didn't notice I was running the application...
作者: luckiejacky 時間: 2015-4-28 16:18
本帖最後由 luckiejacky 於 2015-4-28 16:35 編輯
Can I ask a question?
If my class module has a protected member,
I need to make it public in order to let the Unit test run it
How can I avoid that? Don't want to change it back and forth
from protected to public and public to protected again...
BTW:
How do I maintain the database state after each test so that
the db is brought back to the original state after each test?
THX
作者: l0001 時間: 2015-4-28 21:53
提示: 作者被禁止或刪除 內容自動屏蔽
作者: luckiejacky 時間: 2015-4-29 14:05
本帖最後由 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);
- }
- }
作者: luckiejacky 時間: 2015-4-30 21:24
Can I do something like this? Just a thought...
- public int testInsertMethod(.....) {
- return Insert_Record(....);
- }
作者: l0001 時間: 2015-5-9 01:58
提示: 作者被禁止或刪除 內容自動屏蔽
