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 3613360
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:06:52+00:00 2026-05-18T22:06:52+00:00

I have read so many places is that if your code is not test-able

  • 0

I have read so many places is that if your code is not test-able that mean code is not well written. So that makes me start writing a code that is test-able and to start using some unit testing framework.

With this though I start looking for some example with piece of code that is not testable and gradually converted to a testable code. I find tons of examples on unit testing but if someone can provide an example like above it probably can jump start things for me.

TIA

  • 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-05-18T22:06:52+00:00Added an answer on May 18, 2026 at 10:06 pm

    Put a bunch of code in a button click event and try to unit test it. It’s not impossible, but will either be non-trivial or require some copy-paste finagling to get it done.

    protected void buttonClick(object sender, EventArgs e)
    {
        string currUser =
            User.Identity.Name.ToString().Trim()
                .Substring(User.Identity.Name.ToString().Trim()
                .IndexOf("\\") + 1);
    
        Inventory.Employee.DB objEmpDB = new Inventory.Employee.DB();
        Inventory.Employee.Details objEmpDetails = 
            new Inventory.Employee.Details();
    
        objEmpDetails = objEmpDB.Get(currUser);
    
        Welcome.Text = 
            "Current User: " + objEmpDetails.Employee_Full_Name;
    
        var objUserDetails = new Inventory.User.Details();
        Inventory.User.DB objUserDB = new Inventory.User.DB();
    
        if (objUserDB.UserAuthenticates(currUser))
        {
            objUserDetails = objUserDB.Get(currUser);
            currUserToken = objUserDetails.User_Token.Value;
    
            userID.Text = currUser;
    
            if (objUserDetails.Active_User_Name != objUserDetails.User_Name)
            {
                lShadow.Text = "Showin: " + objUserDetails.Active_User_Name;
                lServer.Text = "(" +
                objUserDB.UserPermissionName(objUserDetails.Active_Logon_Name)
                    + ") - " + System.Environment.MachineName;
                lShadow.ToolTip = Inventory.Properties.Settings.Default
                    .connectionString.Substring(0, Inventory.Properties
                    .Settings.Default.connectionString.IndexOf(';'));
                divShadow.Visible = true;
            }
            else
                divShadow.Visible = false;
    
            lWelcome.Text = "Current User: " + objUserDetails.User_Name;
        }
    }
    

    Not only is this hard because of the difficulty of emulating a user button click, but look how much is going on in that button click. If your unit test fails, there’s about 100 freakin things that could have gone wrong. DRY, single concern, and other design principles lead to code that’s easy to test and easy to fix. After all, what good is a unit test if you are testing brigades rather than units 🙂

    UPDATE: (How to fix the above code)
    I’m not going to pretend that the code above is an easy fix. That’s a “small” sample from a code base I’ve worked on in the past. I wanted to show how bad things can get in real life.

    There’s two major problems with the code.

    1. Its hard to test button click
      events.
    2. There’s too much going
      on in one method.

    Its easy to fix the Event driven/reproducing a button click event problem. You can wrap all that code into another method:

    protected void buttonClick(object sender, EventArgs e)
    {
       EasyToCallMethod();
    }
    
    public void EasyToCallMethod()
    {
        string currUser =
            User.Identity.Name.ToString().Trim()
            .Substring(User.Identity.Name.ToString().Trim().IndexOf("\\") + 1);
        //...rest of code
    }
    

    Now its easy to call from a unit test. But, that’s a little silly because it really doesn’t solve the second problem.

    Easy Fix
    So there’s a good 15-20 tests that we can make out of this one method call. Just make a test for each line that has a specific purpose (like where method calls are made) and you should have good unit tests that are small enough to tell where something broke and good code coverage.
    Advanced stuff
    Much more work can be done. We can implement n-tier MVC or MVVM . At some point, you have to ask yourself if you are over-engineering. Unit tests should make your code more maintainable, but don’t over-abstract yourself into nothingness. This is where your own style and experience come into play. When you feel like you’ve got the basics you should come back to SO with new questions or pickup a good book.

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

Sidebar

Related Questions

I have read in many places that WPF combo does not support autocomplete but
i have read many articles that can be found on soap , i am
Alright...I've given the site a fair search and have read over many posts about
I have read a lot that LISP can redefine syntax on the fly, presumably
I have read that using database keys in a URL is a bad thing
I have read this post about how to test private methods. I usually do
I have read on Stack Overflow some people that have converting to C#2.0 to
I have read (or perhaps heard from a colleague) that in .NET, TransactionScope can
I have based many designs and frameworks that use C# Attributes over the past
I have read many posts on Session-scoped data in MVC, but I am still

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.