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

The Archive Base Latest Questions

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

Basically, here’s the situation. I have the following: public IService Service { get; set;

  • 0

Basically, here’s the situation. I have the following:

public IService Service { get; set; } //Set to MyMockedService class.

public Boolean DoFoo()
{
    //possible other ways of returning true/false...

    Boolean success = true;

    //Get FileInfo[] items

    foreach (var item in items)
        DoOtherFoo(item);
}

public Boolean DoOtherFoo(FileInfo fileInfo)
{
    String filepath = //manipulate fileInfo.FullName;

    Byte[] file = Service.GetFile(filepath)
    try
    {
        WriteBinaryFile(filepath, file); //How can I force file writing to throw an exception
    }
    catch (Exception)
    {
        return false;
    }
}

Basically, In testing DoFoo() I have a lot of paths that could return true/false. I’ve unit tested all of them except the final one… where it attempts to write the files and if even one of the files can’t be written for some reason, it fails and sends back false. At first I figured if I tried to setup a bad filename such as “bad*file” it would throw an exception in the WriteFile but I didn’t even get that far because I can’t create a FileInfo object using illegal characters. So I’m looking for another way to make it so that it would be impossible to write the file so I can get back a false.

  • 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-19T03:24:54+00:00Added an answer on May 19, 2026 at 3:24 am

    Good unit tests are isolated ones. It means they are absolutely idependent of any environment (files, database, network etc.). If your code uses files to store data in it, you should hide by some interface, the production and test code would use different implmementation. Production will really do Write to file, test one will only emulate it.

    public interface IStorage
    {
      bool StoreToFile(string path, string file, byte[] data);
    }
    
    public class Storage : IStorage
    {
      public bool StoreToFile( ... )
      {
         return WriteToFile( ... );
      }
    }
    
    public class StorageMock : IStorage
    {
       public bool StoreToFile (...)
       {
          return false;  //or true, depends on you test case
       }
    }
    

    Now in tests you are able to “subtitue” real implmementaion with fake one. This is called mocking.

    Desing that takes into considiration called “Inversion of Control”. There are also a bunch of frameworks that allows you to use “Investion of Control” throught dependency injection (StructureMap, Ninject, Wisdor)

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

Sidebar

Related Questions

Okay basically here's where I'm at. I have a list of PropertyDescriptor objects. These
I'm seeing basically the same issue described here I have a table that starts
I have some code here: (It basically checks if a product is a kit
Currently what I have coded up works, but it seems rather crude. Basically here's
Good afternoon, Wow what a title. Basically here is the thing. If have a
Hey everyone, not sure what is going on here :( Basically I have a
So basically here's my problem. I'm looking for a solution to allow us to
Basically, here's my problem. I'm calling someone else's FORTRAN functions from my C++ code,
Here is the fiddle: fiddle Basically I want to include and accordion menu using
Here is my code... basically it finds any page-NUMBER- within a variable and then

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.