Reputation: 665
Apparently i have been struggling to test an application built by Developers in my Ourteam.
The Class:Apis, Reads the connection string,id and error message from user.I return a dictionary of values name, and Qualification weight.Because am a newbie in Testing am facing alot problem to get my test working as expected.Please advise where am not doing wrong.
Class:
public class Apis
{
public Dictionary<String, String> getQualWeight(String sqlConStr, String inBin, Label lblResults)
{
Dictionary<String, String> qualList = new Dictionary<string, string>();
string selectSQL = "select Name,qual_weight from Qualification_type "
+ "where ID in (select Qualification_ID from Qualifications where BIN = @inBin)";
con = getConn(sqlConStr);
SqlCommand cmd = new SqlCommand(selectSQL, con);
cmd.Parameters.AddWithValue("@inBin", inBin);
SqlDataReader reader;
try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
qualList.Add(reader[0].ToString(), reader[1].ToString());
}
reader.Close();
return qualList;
}
catch (Exception err)
{
lblResults.Text = "error fetching qualification weight " + err.Message;
return null;
}
finally
{
con.Close();
}
}
My test:
[TestMethod()]
public void getQualWeightTest()
{
Api target = new Api();
string sqlConStr = "SERVER=ABC123; Database=DB; UID=id; PWD=passme;encrypt=no;enlist=false";
string inBin = "2012-52-456";
Label lblResults = null;
lblResults.Text = " Failed";
Dictionary<string, string> expected = new Dictionary<string,string>();
expected.Add("Gohn", "50");
Dictionary<string, string> actual;
actual = target.getQualWeight(sqlConStr, inBin, lblResults);
Assert.AreEqual(expected, actual);
}
Upvotes: 0
Views: 1343
Reputation: 36081
Just looking at your code I have a couple of suggestions.
I find a good indicator of how decoupled your code is is how easy it is to test. If your having difficulty testing your code, it could be a candidate for refactoring.
Upvotes: 2