0
public int validation()
{
    int flag = 0;
    Regex Rx = new Regex(@"^[\p{L} \.'\-]{0,20}$");
    Regex Rx1 = new Regex(@"^[\p{L} \.'\-]{0,20}$");
    Regex Rx2 = new Regex(@"^[0-9]{10}$");
    if (name.Text=="")
    {
        name.Focus();
        errorProvider1.SetError(name, MessageBox.Show("enter your name", 
            "error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
        flag = 1;
    }
    else if (fathername.Text == "")
    {
        fathername.Focus();
        errorProvider1.SetError(name, MessageBox.Show("Enter your father name", 
            "error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
        flag = 1;
    }
}

其 Windows 窗体的公共验证部分代码

4

2 回答 2

1

要编写有效的单元测试,首先你的代码应该是可测试的。要首先拥有一段可测试的代码,您应该将业务与 UI 分开。通过这种方式,您可以通过单元测试来测试您的业务。此外,如果您使用 MVVM 模式,您可以独立于您的 UI 技术测试您的 UI 逻辑。

于 2019-01-20T05:33:05.483 回答
0

单元测试步骤的共同点是。Arrange -> Act -> Assert 在我看来,我应该用单独的方法编写你的代码来验证。该方法将返回真或假。检查和单元测试很容易。问候。

public bool ValidateInput(string input)
{
  Regex Rx = new Regex(@"^[\p{L} \.'\-]{0,20}$");
  Match match = rx.Match(input);
  if(match.Success) return true;
  return false;
}
// To call the method.
public void Testing(){
  string userName = "your user name";
  if(ValidateInput(userName))
    MessageBox.Show("Your username incorrect.");
  else{
    // Todo something.
  }
}
于 2019-01-20T04:39:11.140 回答