Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8284359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:04:45+00:00 2026-06-08T11:04:45+00:00

Apparently i have been struggling to test an application built by Developers in my

  • 0

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

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:04:46+00:00Added an answer on June 8, 2026 at 11:04 am

    Just looking at your code I have a couple of suggestions.

    1. You don’t want to rely on an external resource (in this case the Database) in your unit tests. Your code could be fine but the test could fail if this database is down. Look at something like the Repository Pattern and pass in (or ideally inject using Dependency Injection) a repository rather than a connection string. Using this approach you can use a ‘Fake’ repository and you have complete control over the data being passed back and you remove the dependency on the database. You can also test your repository in isolation if need be.
    2. This code looks like it is in violation of the Single Responsibility Principle, it is responsible for Data Access and updating the UI. Calling out to a repository would remove the data access from the code and make it easier to test.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an apparently easy problem which is: <div class=container> <div class=a></div> <div class=b></div>
A project I have been working on for several years apparently has gotten so
I have been having trouble with Hibernate and Mysql timeout error for a week.
I have been required to write a function that reads the BSDF data format
I'm getting a Variable TMP_1 might not have been initialized error. Here's the snippet:
I have been looking at this error for quite some time, and can't seem
We have been successfully using protobuf-net v1 in a compact framework application to handle
I have been developing an application with Apache Felix as my OSGi runtime for
I've (apparently) checked-in (added) a file that shouldn't have been added to a branch.
I have been using Affine Transform to rotate a String in my java project,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.