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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:45:17+00:00 2026-05-28T00:45:17+00:00

I have an application that takes a dictionary of files (file type, and list

  • 0

I have an application that takes a dictionary of files (file type, and list of file names) and copies the files from the original directory into another location.
I’ve already got the basic code for the copy process, but I need to do some unit tests so it is as robust as possible.

I have wrapper class that I am using so I can test that the System.IO methods are called as I expect, but I am having some difficulty figuring out how to form the tests as there are foreach and switch statements in the code.
Sample code below:

private IFileSystemIO _f;


public CopyFilesToDestination(IFileSystemIO f){
    _f = f;
}


public void Cpy_Files(Dictionary<string, List<string>> files)
{
// get a list of the file types in the directory
var listOfFileTypes = new List<string>(files.Keys);

foreach (var fileType in listOfFileTypes){
    var fileList = files[fileType].ToList();

    foreach  (var file in fileList){
        switch(fileType){
            case ".txt":
                _f.Copy(file, @"c:\destination\text");
                break;
            case ".dat":
                _.Copy(file, @"c:\destination\data");
                break;
        }
    }
}
}

To test the above I had thought I would use a mock dictionary object, set up with a list of file types and paths:

public virtual Dictionary<string, List<string>> FakeFiles(){
    return fakeDictionary = new Dictionary<string, List<string>>(){
        {".txt",  new List<string>(){
            "c:\test\file1.txt",
                "c:\test\file2.txt"
            }
        },
        {".dat", new List<string>(){
                "c:\test\file1.dat",
                "c:\test\file2.dat"
            }
        };
    }
}

The first test I came up with looks like this:

[Test]
public void Should_Copy_Text_Files(){
    var dictionary = new FakeDictionary().FakeFiles();

    var mockObject = MockRepository.GenerateMock<IFileSystemIO>();
    var systemUnderTest = new CopyFileToDestination(mockObject);

    systemUnderTest.Cpy_Files(dictionary);

    // I think this means "test the operation, don't check the values in the arguments"      but I also think I'm wrong
    mockObject.AssertWasCalled(f => f.Copy("something", "something"), o =>     o.IgnoreArguments());

}

My first problem is: How do I test for a specific file type, such as “.txt”?
Then how do I test the loops? I know with the mocked dictionary that I only have two items, do I leverage this to form the test? How?

I think I may be close to a solution, but I am running out of time/patience hunting it down. Any help is greatly appreciated.
Thanks
Jim

  • 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-28T00:45:17+00:00Added an answer on May 28, 2026 at 12:45 am

    I tried using Roberts solution, but as I stated, I have too many different file types to set up each test case individually. The next thing I tried was setting up a TestCaseSource, but every time I ran the test for that it marked the test as ignored:

    [Test, TestCaseSource(typeof(FakeDictionary), "TestFiles")]
    public void Cpy_Files_ShouldCopyAllFilesInDictionary(string extension, string fielName)    {
        // Arrange
        var mockObject = MockRepository.GenerateMock<IFileSystemIO>();
        var systemUnderTest = new CopyFileToDestination(mockObject);
    
        // Act
        systemUnderTest.Cpy_Files(dictionary);
    
        // Assert
        mockObject.AssertWasCalled(f => f.Copy(extension, fileName));
    }
    

    The data source is below:

    public static Dictionary<string, string> TestFiles{
        get
        {
            return new Dictionary<string, string>()
                {
                    {".txt",
                    "C:\\test\\test1.txt"},
                    {".txt",
                    "c:\\test\\test2.txt"}
                };
        }
    } 
    

    What I finally worked out uses the times to repeat option in Rhino and is really pretty simple:

    [Test]
    public void Cpy_Files_ShouldCopyAllFilesInDictionary(){
        // Arrange
        var mockObject = MockRepository.GenerateMock<IFileSystemIO>();
        var systemUnderTest = new CopyFileToDestination(mockObject);
    
        // Act
        systemUnderTest.Cpy_Files(dictionary);
    
        // Assert
    
        // I know how many objects are in my fake dictionary so I set the times to repeat as a const
        const int timesToRepeat = 2;
    
        // now I just set the values below. I am not testing file names so the test will ignore arguments
        mockObject.AssertWasCalled(f => f.Copy("",""), options => options.Repeat.Times(timesToRepeat).IgnoreArguments());
    }
    

    I hope this helps someone else with a similar problem.

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

Sidebar

Related Questions

I have an application that takes some input and generates configuration files as output.
I have a custom routing application that takes information for a route from google
I have a WCF application, written in VB.NET that takes a generic Dictionary(Of String,
I have a C#-Application that stores data from a TextFile in a Dictionary-Object. The
I have a subroutine of a Python application that takes a list of eight-character
I have a proprietary application that takes XML files and can use embedded Java
I have an application that takes a couple of seconds to run. Is it
I have a single-view application that takes in a username and a password and
If I have a 3 layer web forms application that takes user input, I
We have a web application that produces reports. Data are taken from a database.

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.