怎樣在Visual Studio Community 2013 加 Unit Test Project?

Hi
因在Project Startup 時無加Unit Testing Module
唔知點可以後加番落去...
THX

附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

提示: 作者被禁止或刪除 內容自動屏蔽

TOP

Thanks, I didn't notice I was running the application...

TOP

本帖最後由 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

TOP

提示: 作者被禁止或刪除 內容自動屏蔽

TOP

本帖最後由 luckiejacky 於 2015-4-29 14:19 編輯

How can I make this class Unit-Testable?
THX

  1. namespace RCS.followupperson
  2. {
  3.     public partial class CompleteTask : System.Web.UI.Page
  4.     {     

  5.         protected void Page_Load(object sender, EventArgs e)
  6.         {         
  7.         }

  8.         protected void FormView1_PreRender(object sender, EventArgs e)
  9.         {
  10.             if (FormView1.CurrentMode == FormViewMode.Insert)
  11.             {

  12.                 Label ItemNo = (Label)FormView1.FindControl("ItemNoLabel1");
  13.                 ItemNo.Text = Request["ItemNo"].ToString();
  14.                 Int32 itemNo = Int32.Parse(ItemNo.Text);              

  15.                 // Fill the last revised date information into the formview
  16.                 DateTime OriginalTargetDate = ReadOriginalTargetDateInfo(itemNo);

  17.                 TextBox Ori_Target_Finish_Date = (TextBox)FormView1.FindControl("Ori_Target_Finish_DateTextBox");

  18.                 Ori_Target_Finish_Date.Text = OriginalTargetDate.ToString("dd-MMM-yyyy");
  19.             }
  20.         }

  21.         protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
  22.         {

  23.             using (var scope = new TransactionScope())
  24.             {
  25.                 Int32 newAppID = -1;

  26.                 Label itemNoLabel = (Label)FormView1.FindControl("ItemNoLabel1");
  27.                 TextBox txtFinishDate = (TextBox)FormView1.FindControl("Finish_DateTextBox");

  28.                 Int32 ItemNo = Convert.ToInt32(itemNoLabel.Text);
  29.                 DateTime FinishDate = DateTime.Parse(txtFinishDate.Text);

  30.                 TextBox reasonTextBox = (TextBox)FormView1.FindControl("ReasonTextBox");
  31.                 String FinishReason = reasonTextBox.Text;

  32.                 Approvals app = new Approvals();
  33.                 app.ItemNo = ItemNo;
  34.                 app.AppType = ApprovalType.FINISH_JOB;
  35.                 app.CreatedBy = new Guid(Session["userId"].ToString());
  36.                 app.ProposedPersonnelID = new Guid(Session["userId"].ToString());
  37.                 app.ProposedDate = DateTime.Parse(txtFinishDate.Text);
  38.                 app.ApprovePersonID = Guid.Empty;
  39.                 app.ApprovalDate = (DateTime?)null;
  40.                 app.ApprovalReason = "";
  41.                 // 1) Insert Finish Job Approval for approved tracking
  42.                 Helpers.InsertApprovalsWithReturn(app, ref newAppID);

  43.                 // 2) Insert Finish Job Log for awaiting approval tracking
  44.                 InsertFinishTargetLog(newAppID, ItemNo, FinishDate, FinishReason);

  45.                 String FollowupPersonEmail = "";
  46.                 String SupervisorEmail = "";

  47.                 Helpers.GetEmailAddresses(new Guid(Session["userId"].ToString()), ref FollowupPersonEmail, ref SupervisorEmail);

  48.                 // 3) Send an Email notification to the followup person
  49.                 Helpers.SendingMail(FollowupPersonEmail, SupervisorEmail, "Finish Job", "Finish Job");
  50.                 scope.Complete();
  51.             }
  52.             Response.Redirect("~/followupperson/ViewIssue.aspx");
  53.         }

  54.         private DateTime ReadOriginalTargetDateInfo(Int32 ItemNo)
  55.         {
  56.             SqlConnection con = new SqlConnection();
  57.             con.ConnectionString = ConfigurationManager.ConnectionStrings["RCS_2_02ConnectionString"].ConnectionString;

  58.             // Read the latest revised target finish date
  59.             SqlCommand comm = new SqlCommand(
  60.                  @"SELECT i.[ItemNo],  
  61.                           max(i.[OriTargetFinishDate]) AS OriTargetFinishDate
  62.                             FROM [IssueLog] AS i
  63.                             WHERE           
  64.                               (i.[ItemNo] = @ItemNo)
  65.                             group by i.[ItemNo]
  66.                            ", con);
  67.             con.Open();
  68.             comm.Parameters.AddWithValue("@ItemNo", ItemNo);
  69.             SqlDataReader rdr = comm.ExecuteReader();            
  70.             if (rdr.HasRows)
  71.             {
  72.                 rdr.Read();
  73.                 DateTime OriTargetFinishDate = DateTime.Parse(rdr["OriTargetFinishDate"].ToString());
  74.                 rdr.Close();
  75.                 con.Close();
  76.                 return OriTargetFinishDate;
  77.                
  78.             }
  79.             con.Close();
  80.             return DateTime.MinValue;            
  81.             
  82.         }
  83.         
  84.         protected int InsertFinishTargetLog(Int32 newAppID, Int32 ItemNo, DateTime FinishDate, String FinishReason)
  85.         {      
  86.                String ComText = @"INSERT INTO FinishTargetLog
  87.                        (AppID, ItemNo, FinishDate, FinishReason)
  88.                        VALUES (@AppID, @ItemNo, @FinishDate, @FinishReason)";
  89.                 try
  90.                 {
  91.                     using (var con = new SqlConnection())
  92.                     {
  93.                         con.ConnectionString = ConfigurationManager.ConnectionStrings
  94.                                                     ["RCS_2_02ConnectionString"].ConnectionString;
  95.                         con.Open();
  96.                         {
  97.                             using (var comm = new SqlCommand(ComText, con))
  98.                             {
  99.                                 comm.Parameters.AddWithValue("@AppID", newAppID);
  100.                                 comm.Parameters.AddWithValue("@ItemNo", ItemNo);
  101.                                 comm.Parameters.AddWithValue("@FinishDate", FinishDate.ToShortDateString());
  102.                                 comm.Parameters.AddWithValue("@FinishReason", FinishReason);

  103.                                 int res = comm.ExecuteNonQuery();
  104.                                 con.Close();
  105.                                 return 0;
  106.                             }
  107.                         }                        
  108.                     }                    
  109.                 }
  110.                 catch (Exception ex)
  111.                 {                     
  112.                     throw new Exception(ex.Message);
  113.                 }            
  114.         }
複製代碼

TOP

Can I do something like this? Just a thought...
  1. public int testInsertMethod(.....) {
  2.         return Insert_Record(....);
  3. }
複製代碼

TOP

提示: 作者被禁止或刪除 內容自動屏蔽

TOP