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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:13:40+00:00 2026-06-06T06:13:40+00:00

Is it possible to mock out File calls with rhino mock example: private ServerConnection

  • 0

Is it possible to mock out File calls with rhino mock example:

  private ServerConnection LoadConnectionDetailsFromDisk(string flowProcess)
  {
       var appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
       var bodyFile = Path.Combine(appPath, @"XML\ServerConnections.xml");

        if (File.Exists(bodyFile))
        {
            //more logic
  }

So I am trying to mock the File.Exists method so it will return true, so I am able to test the next branch of logic regardless of if the file exists or not. 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-06T06:13:41+00:00Added an answer on June 6, 2026 at 6:13 am

    Here’s your original snippet:

    private ServerConnection LoadConnectionDetailsFromDisk(string flowProcess)
    {
       var appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
       var bodyFile = Path.Combine(appPath, @"XML\ServerConnections.xml");
    
        if (File.Exists(bodyFile))
        {
            //more logic
        }
    }
    

    Instead of using the System.IO library (which is not possible to mock), cadrell was basically saying to add a layer of abstraction, which you can mock:

    private ServerConnection LoadConnectionDetailsFromDisk(string flowProcess)
    {
       var appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
       var bodyFile = Path.Combine(appPath, @"XML\ServerConnections.xml");
    
        if (FileExists(bodyFile))
        {
            //more logic
        }
    }
    
    public bool FileExists(bodyFile) { return File.Exists(bodyFile) }
    

    Now, in your test, you can define a PartialMock that uses most of the existing code (allowing you to test it) but allows you to override just the FileExists method:

    var myPartialMock = mockRepo.PartialMock(typeof(MyObject));
    
    myPartialMock.Expect(m=>m.FileExists("")).IgnoreArguments().Return(true);
    
    myPartialMock.LoadConnectionDetailsFromDisk("myProcess");
    

    Now, the call from inside your if statement always returns true.

    Something else to consider; I see an if block predicated on the existence of a file. You didn’t specify the code, but I would bet anybody else but you (since you can change the code) that the code opens or manipulates the file we now know exists. So, the entire method rubs right up against the border of what you can and can’t unit-test. You can consider refactoring this method to obtain a Stream from another function (allowing you to mock that function and inject a MemoryStream with test data), but at some point you’ll be scraping the edges of your “sandbox” and will just have to trust that the .NET team did their job and that calls to File.Exists, File.Open etc work as expected.

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

Sidebar

Related Questions

Possible Duplicate: How do you mock out the file system in C# for unit
Is it possible to mock method calls from within other method calls of the
Wherever possible I use TDD: I mock out my interfaces I use IOC so
Is it possible to mock a static method using Rhino.Mocks? If Rhino does not
Is it possible to mock (with mockito) method with signature Set<? extends Car> getCars()
Is it possible to mock the Assembly class? If so, using what framework, and
Is it possible to mock an object in a way that it fakes multiple
I was wandering if it's possible to mock a Game object in order to
Possible Duplicate: How to mock a method that returns an int with MOQ Here's
Possible Duplicate: Tools for Generating Mock Data? I am running SQL Server 2005 and

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.