yuvaraj
yuvaraj

Reputation: 11

how to do unit testing for this type of code in windows form c#

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;
    }
}

its public validation part code for windows form

Upvotes: 0

Views: 69

Answers (2)

mehrandvd
mehrandvd

Reputation: 9116

To write effective unit tests, first your code should be testable. To have a testable piece of code first you should separate the business from UI. In this way you can test your business via unit testing. Also if you use MVVM patterns you can test your UI logic independently from your UI technology.

Upvotes: 1

NP90s
NP90s

Reputation: 23

The common of unit testing steps is. Arrange -> Act -> Assert In my opinion, I should write your code in separate method to validate. The method will return true or false. It's easy to check and unit testing. regards.

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.
  }
}

Upvotes: 0

Related Questions