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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:31:26+00:00 2026-05-24T03:31:26+00:00

Possible Duplicate: .NET file system wrapper library I would like to write a test

  • 0

Possible Duplicate:
.NET file system wrapper library

I would like to write a test where the content of a file get’s loaded. In the example the class which is used to load the content is

FileClass

and the method

GetContentFromFile(string path).

Is there any way to mock the

File.exists(string path)

method in the given example with moq?

Example:

I have a class with a method like that:

public class FileClass 
{
   public string GetContentFromFile(string path)
   {
       if (File.exists(path))
       {
           //Do some more logic in here...
       }
   }
}
  • 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-24T03:31:27+00:00Added an answer on May 24, 2026 at 3:31 am

    Since the Exists method is a static method on the File class, you can’t mock it (see note at bottom). The simplest way to work around this is to write a thin wrapper around the File class. This class should implement an interface that can be injected into your class.

    public interface IFileWrapper {
        bool Exists(string path);
    }
    
    public class FileWrapper : IFileWrapper {
        public bool Exists(string path) {
            return File.Exists(path);
        }
    }
    

    Then in your class:

    public class FileClass {
       private readonly IFileWrapper wrapper;
    
       public FileClass(IFileWrapper wrapper) {
           this.wrapper = wrapper;
       }
    
       public string GetContentFromFile(string path){
           if (wrapper.Exists(path)) {
               //Do some more logic in here...
           }
       }
    }
    

    NOTE: TypeMock allows you to mock static methods. Other popular frameworks, e.g. Moq, Rhino Mocks, etc, don’t.

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

Sidebar

Related Questions

Possible Duplicate: CSV File Imports in .Net In .net, is there a standard library
Possible Duplicate: .Net Zip Up files I want to zip a .csv file using
Possible Duplicate: What Are Some Good .NET Profilers? I am trying to test an
Possible Duplicate: .NET How to check if path is a file and not a
Possible Duplicate: Lucene.Net and SQL Server I need to storage and index files, like
Possible Duplicate: Writing a CSV file in .net Are there any good CSV writers
Possible Duplicate: Streaming large file uploads to ASP.NET MVC To my understanding, when a
Possible Duplicate: Reading PDF documents in .Net How do I read a pdf file
Possible Duplicate: Determine framework (CLR) version of assembly I have a library/DLL file which
Possible Duplicate: How can I get the application's path in .NET in a console

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.