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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:46:00+00:00 2026-06-10T13:46:00+00:00

I have the following code that I’d like to test: public class DirectoryProcessor {

  • 0

I have the following code that I’d like to test:

public class DirectoryProcessor
{
    public string DirectoryPath
    {
        get;
        set;
    }

    private FileSystemWatcher watcher;

    public event EventHandler<SourceEventArgs> SourceFileChanged;

    protected virtual void OnSourceFileChanged(SourceEventArgs e)
    {
        EventHandler<SourceEventArgs> handler = SourceFileChanged;
        if(handler != null)
        {
            handler(this, e);
        }
    }

    public DirectoryProcessor(string directoryPath)
    {
        this.DirectoryPath = directoryPath;
        this.watcher = new FileSystemWatcher(directoryPath);
        this.watcher.Created += new FileSystemEventHandler(Created);
    }

    void Created(object sender, FileSystemEventArgs e)
    {
        // process the newly created file
        // then raise my own event indicating that processing is done
        OnSourceFileChanged(new SourceEventArgs(e.Name));
    }
}

Basically, I want to write an NUnit test that will do the following:

  1. Create a directory
  2. Setup a DirectoryProcessor
  3. Write some files to the directory (via File.WriteAllText())
  4. Check that DirectoryProcessor.SourceFileChanged has fired once for each file added in step 3.

I tried doing this and adding Thread.Sleep() after step 3, but it’s hard to get the timeout correct. It correctly processes the first file I write to the directory, but not the second (and that’s with the timeout set to 60s). Even if I could get it working this way, it seems like a terrible way to write the test.

Does anyone have a good solution to this problem?

  • 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-10T13:46:01+00:00Added an answer on June 10, 2026 at 1:46 pm

    If you are looking to test another object that uses this class my answer is not relevant.

    When I write unit tests to operations I prefer using the ManualResetEvent

    The unit test will be something like:

         ...
         DirectoryProcessor.SourceFileChanged+=onChanged;
         manualResetEvent.Reset();
         File.WriteAllText();
         var actual = manualResetEvent.WaitOne(MaxTimeout);
         ...
    

    when manualResetEvent is the ManualResetEvent and the MaxTimeout is some TimeSpan (my advice always use the time out).
    now we are missing the “onChanged”:

         private void onChanged(object sender, SourceEventArgs e)
         {
              manualResetEvent.Set();
         }    
    

    I hope this is helpful

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

Sidebar

Related Questions

I have following code that displays an Image with letters, public class MainActivity extends
I have following code that does not work: I never get to goToFoodDetail .
I have following code snippet that i use to compile class at the run
I have the following code that cannot be compiled: #include <iostream> #include <set> #include
I have the following code that captures my 'Team_ID' Get parameters and I'm trying
I have the following code that I have been using to Set properties in
I have the following code that allows me to load a set URL each
I have the following code that should add an active css class to the
I have the following code that takes a String of milliseconds (will be from
I have the following code that decodes a JSON string into an array of

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.