2016-11-01 3 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

참 하하가 들어있는 클래스를 테스트하지 않습니다 논리적 인 것처럼 보입니다. 그렇지 않습니다. 제어 조작과 논리가 너무 얽혀 있기 때문에 이것은 꽤 어려울 것입니다. 그러나 저는 그것을 한 번 해보겠습니다. 방금 내 머리 속에는 사용자 정의 컨트롤이 별도의 클래스없이 자체 논리를 포함 할 수 있다는 생각이 들었지만 내 생각에 오해였습니다. –

관련 문제