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

  • SEARCH
  • Home
  • 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 8432255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:04:47+00:00 2026-06-10T06:04:47+00:00

I have various methods that I would like to unit test using Visual Studio’s

  • 0

I have various methods that I would like to unit test using Visual Studio’s built in unit testing capability for C#. Things have been going pretty smoothly, but I’ve run into a scenario where I want to “stub out” a dependency on a particular function call.

Namely, I have some methods that are in the following format:

public class ClassWithMethodsIWantToUnitTest 
{
    public void myFcn(object someArg)
    {
        ...
        LoggerService.Instance.LogMessage("myMessage");
        ...
    }
}

So basically, I want my unit test to simply verify that the call to “LogMessage” has occurred. I don’t want to actually check a log file or anything. I want a way to see if the LoggerService line has been hit and executed.

LoggerService is a singleton, and if possible, I don’t want to modify the code just for unit testing.

Based on the format of this problem, it seems to me that it should be possible to somehow take control of the code in my unit test. In other words, is there a way for me to make a fake version of LogMessage such that I can use it for my unit test? I don’t want the “real” LogMessage function to be called if possible. I just want to test that the code hit the path that called the function.

Does this make any sense? Is this possible?

  • 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-10T06:04:49+00:00Added an answer on June 10, 2026 at 6:04 am

    Anders’ answer was definitely the “canonical” way of approaching the problem, but in .NET 4.5, there’s a second option:

    The Fakes framework.

    It lets you add a “fakes” assembly to your unit test project that accepts a delegate to perform in place of the actual implementation of a method. Here’s an example using File.ReadAllText

        [TestMethod]
        public void Foo()
        {
            using (ShimsContext.Create())
            {
                ShimFile.ReadAllTextString = path => "test 123";
                var reverser = new TextReverser();
                const string expected = "321 tset";
                //Act
                var actual = reverser.ReverseSomeTextFromAFile(@"C:\fakefile.txt");
                //Assert
                Assert.AreEqual(expected, actual);
            }
        }
    

    What that test method is doing is temporarily (within the scope of the ShimsContext) replacing the implementation of File.ReadAllText with the lambda I provided. In this case, any time ReadAllText is called, it returns the string “test 123”.

    It’s slower than regular DI, but if you’re absolutely tied to a specific implementation of a singleton, it could be exactly what you need. Read more about it here.

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

Sidebar

Related Questions

Lets say you have various objects of arbitrary type that you would like to
I would like to unit test a method on a class I have made,
I have an application that may receive data via various methods and in various
I would like to be able to store various static methods in a List
I have a concept for a framework of controls that I would like to
I have an API (a DLL) which has various methods that return objects /
I have a large multidimensional array that I would like to store into a
I have a network C++ program in Windows that I'd like to test for
I have a class that I would like to share between a Cocoa and
I have a number of classes that call say Trace.WriteLine(LogSomethingClassSpecific), in various methods all

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.