Search code examples
c#unit-testingmstest

If I need to check the parameter of the function?


I'm writing unit test using MSTest in C#.

The end goal of function is to ensure we can get the data.

The function we want to test is like:

public static GetMemberName(string member_id) 
{
  DataTable dt = new DataTable();

  string memberName;

  String sql = @"SELECT name from MEMBER_GROUP where Member_Id = @Member_Id";

  dt = SqlProvider.dbGetDataTable("MY_Table", sql, 

  SqlParameterBuilder.AddParameter("Member_Id ", member_id));

  if ((dt != null) && dt.Rows.Count > 0)
  {
    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
      memberName = dt.Rows[i][0].ToString();
    }
  }

  return memberName;
}

SqlParameterBuilder.AddParameter("Member_Id ", member_id) will add parameter in builder, and make the sql line equals to @"SELECT name from MEMBER_GROUP where Member_Id = "123" when member_id = "123"

I am confused about whether I need to check the sql variable if it is equal to

@"SELECT name from MEMBER_GROUP where Member_Id = @Member_Id"`;

and check if

SqlParameterBuilder.AddParameter("Member_Id ", member_id)

works (in other words, check the parameter of the function dbGetDataTable), or just to fake the return value of dbGetDataTable?

If I need to check it, how can I do that?


Solution

  • Safety net

    You can think about unit testing as developing a safety net. They will try to save you from certain kind of problems. The more test case you have the more safe you are against common pitfalls.

    You can test your code really extensively but they might not caught all unintended changes. The main aim of your safety net should be to catch those "common" unintended changes which has moderate or high probability. So, try to focus your energy to cover those lines of code with test cases which is likely to be amended in the near future.

    Black-Box vs Grey-Box vs White-Box

    Let me quote from this table the 2nd row:

    • Black-Box is also known as functional testing, data-driven testing, and closed box testing.
    • White-Box is also known as structural testing, clear box testing, code-based testing, and transparent testing.
    • Grey-Box is also known as translucent testing as the tester has limited knowledge of coding.

    As you can see the main difference between these approaches how much the test cases "know" about the to-be-tested code. In case of white-box testing the implementation details are also covered with tests. Whereas in case of black-box testing you think about your to-be-tested code as a 3rd party obfuscated method, that's why you only care about input, output and potential side-effect (if any).

    So, if The end goal of function is to ensure we can get the data then we are talking about black-box testing where you have no knowledge about sql query and SqlParameterBuilder.