I am working on introducing unit testing to a class in my project which is responsible for verifying customers according to a specific criteria. For clarity, let's say one of the methods in my class is shown below:
public static MyCustomStatus IsCustomerVerified(int OrderId, int customerId)
{
MyCustomStatus result = null;
//invoke a stored procedure and use its result to initialize 'result' here
using (var conn = new SqlConnection("con_string"))
using (var cmd = new SqlCommand("my_stored_procedure", conn) { CommandType = CommandType.StoredProcedure, CommandTimeout = 120 })
{
cmd.Parameters.AddWithValue("@OrderId", OrderId);
cmd.Parameters.AddWithValue("@customerId", customerId);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (result == null)
{
result = new MyCustomStatus()
{
VerificationStatus = Convert.ToInt32(reader["VerificationStatus"]),
StatusName = reader["StatusName"].ToString(),
ErrorMessage = Convert.ToBoolean(reader["ErrorMessage"])
};
}
}
}
return result;
}
and the MyCustomStatus
class is something like this:
class MyCustomStatus
{
public int VerificationStatus {get;set;}
public string StatusName {get;set;}
public string ErrorMessage {get;set;}
}
I know about the first rule of TDD is first create tests and then implement the methods but as I have already implemented the methods, (and some of them are implemented by other developers), I want to bring unit testing into picture but how? I have read multiple tutorials and seen videos and almost all of them take an example of a simple math operation for example SUM(1,1) should be 2 otherwise test fails. Do I need to revamp my code only then I can use unit tests for this class? If yes, what type of revamping?
Environment: C#, .NET framework, Visual Studio solution