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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:48:37+00:00 2026-06-11T19:48:37+00:00

I have been doing unit testing recently and I’ve successfully mocked various scenarios using

  • 0

I have been doing unit testing recently and I’ve successfully mocked various scenarios using Moq framework and MS Test. I know we can’t test private methods but I want to know if we can mock static methods using Moq.

  • 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-11T19:48:38+00:00Added an answer on June 11, 2026 at 7:48 pm

    Moq (and other DynamicProxy-based mocking frameworks) are unable to mock anything that is not a virtual or abstract method.

    Sealed/static classes/methods can only be faked with Profiler API based tools, like Typemock (commercial) or Microsoft Moles (free, known as Fakes in Visual Studio 2012 Ultimate /2013 /2015).

    Alternatively, you could refactor your design to abstract calls to static methods, and provide this abstraction to your class via dependency injection. Then you’d not only have a better design, it will be testable with free tools, like Moq.

    A common pattern to allow testability can be applied without using any tools altogether. Consider the following method:

    public class MyClass
    {
        public string[] GetMyData(string fileName)
        {
            string[] data = FileUtil.ReadDataFromFile(fileName);
            return data;
        }
    }
    

    Instead of trying to mock FileUtil.ReadDataFromFile, you could wrap it in a protected virtual method, like this:

    public class MyClass
    {
        public string[] GetMyData(string fileName)
        {
            string[] data = GetDataFromFile(fileName);
            return data;
        }
    
        protected virtual string[] GetDataFromFile(string fileName)
        {
            return FileUtil.ReadDataFromFile(fileName);
        }
    }
    

    Then, in your unit test, derive from MyClass and call it TestableMyClass. Then you can override the GetDataFromFile method to return your own test data.

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

Sidebar

Related Questions

I have been doing TDD and was using it more as unit testing than
I have been doing a lot of unit testing lately with mocking. The one
I have been doing a lot a reading about unit testing. Unit testing seems
I have been doing some research on test driven development and find it pretty
I have been doing some research for using MSMQ. Following 2 gave me fundamental
I'm doing unit testing for our WCF RIA services, which have RequiresRole or RequiresAuthentication
I am 'relatively new' to unit-testing and TDD. Only more recently have I completed
I have been trying to get the hang of TDD and unit testing (in
I have been using Moq and initially Rhino Mocks over the last year together
All my unit test classes have been created by Visual Studio 2008 from a

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.