2016-11-01 86 views
1

我有一個實現了MVP模式(被動視圖)的WinForms項目。單元測試在WinForms中動態填充用戶控件

我認爲我與模式的問題,當涉及到用戶的控制,這是我在單元測試過程想通了

我有,我把我的形式作爲事件的結果的用戶控件在我看來被解僱了。該用戶控件根據從視圖中獲得的數字向其自身添加一定數量的標籤,文本框等。最後,它告訴視圖將用戶控件添加到視圖。

我想單元測試這個類的邏輯,因爲那是我認爲最重要的測試。我只是不知道該怎麼做,因爲在這個類中有邏輯和表單控件。我目前使用Moq來創建我的單元測試。

我通常會創建一個Mock對象來表示視圖,然後測試要獨立測試的對象中的方法的實現。但是,因爲我在這個類中創建了控件,所以我不認爲我可以像這樣測試它(不包括.Forms庫)。

我希望有人知道一個解決方案。

編輯:我一直在想我的邏輯和控制操作分開,但我從我已經發布了最初的用戶控件的代碼如下不同的用戶控制功能掙扎。由於我通過控件列表循環,我不知道如何將它分離爲邏輯,只是控制處理。

用戶控制代碼

public partial class DetailScreenUserControl : UserControl 
{ 
    // Private members. 
    private readonly IDetailScreenView _view; 
    private List<ComboBox> maturityInput = new List<ComboBox>(); 
    private List<ComboBox> complianceInput = new List<ComboBox>(); 

    // Public members. 
    public List<string> MaturityInput 
    { 
     get 
     { 
      var list = new List<string>(); 
      for (int i = 0; i < maturityInput.Count; i++) 
      { 
       list.Add(maturityInput[i].Text); 
      } 
      return list; 
     } 
     set 
     { 
      for (int i = 0; i < maturityInput.Count; i++) 
      { 
       maturityInput[i].DataSource = new List<string>(value); 
      } 
     } 
    } 
    public List<string> ComplianceInput 
    { 
     get 
     { 
      var list = new List<string>(); 
      for (int i = 0; i < complianceInput.Count; i++) 
      { 
       list.Add(complianceInput[i].Text); 
      } 
      return list; 
     } 
     set 
     { 
      for (int i = 0; i < complianceInput.Count; i++) 
      { 
       complianceInput[i].DataSource = new List<string>(value); 
      } 
     } 
    } 

    // Initialize user control with IDetailScreenView. Subscribe to necessary events. 
    public DetailScreenUserControl(IDetailScreenView view) 
    { 
     InitializeComponent(); 
     _view = view; 
     _view.InitializingUserControl += InitializeUserControl; 
    } 

    // Initializes the user control for the detail screen. 
    public void InitializeUserControl(object sender, EventArgs e) 
    { 
     List<string> qStandards = _view.SelectedQuestionStandards; 
     Controls.Clear(); 
     maturityInput.Clear(); 
     complianceInput.Clear(); 

     int inputSeparation = Height/2; 
     int spacing = Width/20; 

     Size = new Size(_view.RightUserControlBoundary - Location.X, Size.Height); 

     for (int i = 0; i < qStandards.Count; i++) 
     { 
      Panel inputPanel = new Panel(); 
      inputPanel.BackColor = Color.AliceBlue; 
      inputPanel.Location = new Point(0, i * inputSeparation); 
      inputPanel.Size = new Size(Width - spacing, inputSeparation); 
      Controls.Add(inputPanel); 

      Label qs_label = new Label(); 
      qs_label.AutoSize = true; 
      qs_label.Location = new Point(0, 0); 
      qs_label.Font = new Font("Arial", 12F, FontStyle.Bold); 
      qs_label.AutoSize = true; 
      qs_label.Text = qStandards[i].ToString(); 
      inputPanel.Controls.Add(qs_label); 

      Label m_label = new Label(); 
      m_label.AutoSize = true; 
      m_label.Location = new Point(0, qs_label.Bounds.Bottom + qs_label.Height/2); 
      m_label.Font = new Font("Arial", 12F, FontStyle.Regular); 
      m_label.Text = "Maturity standard"; 
      inputPanel.Controls.Add(m_label); 

      Label c_label = new Label(); 
      c_label.AutoSize = true; 
      c_label.Location = new Point(0, m_label.Bounds.Bottom + qs_label.Height/2); 
      c_label.Font = new Font("Arial", 12F, FontStyle.Regular); 
      c_label.Text = "Compliance standard"; 
      inputPanel.Controls.Add(c_label); 

      ComboBox m_input = new ComboBox(); 
      m_input.AutoSize = true; 
      m_input.Location = new Point(c_label.Bounds.Right + 2 * spacing, m_label.Bounds.Top); 
      m_input.Font = new Font("Arial", 10F, FontStyle.Regular); 
      m_input.DropDownStyle = ComboBoxStyle.DropDownList; 
      m_input.Size = new Size(inputPanel.Size.Width - m_input.Bounds.Left, spacing); 
      maturityInput.Add(m_input); 
      inputPanel.Controls.Add(m_input); 

      ComboBox c_input = new ComboBox(); 
      c_input.AutoSize = true; 
      c_input.Location = new Point(c_label.Bounds.Right + 2 * spacing, c_label.Bounds.Top); 
      c_input.Font = new Font("Arial", 10F, FontStyle.Regular); 
      c_input.DropDownStyle = ComboBoxStyle.DropDownList; 
      c_input.Size = new Size(inputPanel.Size.Width - c_input.Bounds.Left, spacing); 
      complianceInput.Add(c_input); 
      inputPanel.Controls.Add(c_input); 

     } 

     if(qStandards.Count != 0) 
     { 
      saveAssessmentButton.BackColor = System.Drawing.SystemColors.ButtonHighlight; 
      Controls.Add(saveAssessmentButton); 
      saveAssessmentButton.Location = new Point(this.Size.Width - saveAssessmentButton.Width - spacing, qStandards.Count * inputSeparation); 
     } 

     _view.AddUserControl(); 
    } 

    // Tells the view to save the assessment. 
    private void saveAssessmentButton_Click(object sender, EventArgs e) 
    { 
     _view.SaveAssessmentButtonClicked(); 
    } 
} 

其他用戶控制功能( '答案' 是控件的列表)進行單元測試在此邏輯

public void SaveResults() 
{ 
    results = new List<string>(); 
    int questionNr = 0; 

    for (int p = 0; p < questions.Count; p++) 
    { 
     for (int i = 0; i < questions[p].Count; i++) 
     { 
      bool unanswered = true; 

      results.Add(questions[p][i]); 

      for (int j = 1; j <= maturityAnswers[p].Count; j++) 
      { 
       var radioButton = (RadioButton)answers[questionNr][j]; 
       if (radioButton.Checked) 
       { 
        results.Add(answers[questionNr][j].Text); 
        unanswered = false; 
       } 
      } 
      if (unanswered == true) 
      { 
       results.Add(""); 
      } 
      unanswered = true; 

      for (int j = maturityAnswers[p].Count + 1; j <= (maturityAnswers[p].Count + complianceAnswers[p].Count); j++) 
      { 
       var radioButton = (RadioButton)answers[questionNr][j]; 
       if (radioButton.Checked) 
       { 
        results.Add(answers[questionNr][j].Text); 
        unanswered = false; 
       } 
      } 
      if (unanswered == true) 
      { 
       results.Add(""); 
      } 

      results.Add(answers[questionNr][0].Text.Replace("'", "''")); 

      questionNr++; 
     } 
    } 

回答

0

我想因爲那是我認爲測試最重要的東西。我只是不知道如何做到這一點, 因爲在這類邏輯和表單控件

所以它們分開到不同的類別和測試只包含邏輯

+0

哈哈是啊,類似乎是合乎邏輯的事情,是不是; p。這只是因爲控制操作和邏輯交織在一起非常困難,但我會放棄它。我只是有一個想法,我的腦海中,用戶控件可以包含自己的邏輯沒有一個單獨的類,但我想這是一個誤解我的名義。 –