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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:17:34+00:00 2026-05-26T15:17:34+00:00

I am new to mocking and trying out moq for the first time. I

  • 0

I am new to mocking and trying out moq for the first time. I am wondering if I can write a test only using mocks?

public class FileCopierTests
{
    private string path = AppDomain.CurrentDomain.BaseDirectory;

    [Fact]
    public void Copy_starts_copying_when_event_is_fired_returns_true()
    {
        var fullyQualifiedFileName = string.Format(@"{0}\..\..\Integration\TestData.xls", this.path);
        var destFileName = Path.GetTempPath() + "66768c06-d1c4-4416-be81-767f36abeeb1.xls";

        var copierMock = new Mock<IFileCopier>();
        var watcherMock = new Mock<IFileWatcher>();

        copierMock.Setup(cp => cp.Copy(fullyQualifiedFileName, destFileName)).Raises(
            ev => ev.CopyingFinished += null, destFileName);

        watcherMock.Object.Changed += arg => copierMock.Object.Copy(arg, destFileName);   

        watcherMock.Raise(e => e.Changed += null, fullyQualifiedFileName);

        copierMock.VerifyAll();
    }
}

These are the classes and interfaces I want to test,

public interface IFileCopier
{
    event Action<string> CopyingFinished;

    void Copy(string fqFileName, string destFileName);
}

public class FileCopier : IFileCopier
{
    private ReaderWriterLockSlim @lock = new ReaderWriterLockSlim();

    public event Action<string> CopyingFinished;

    public void Copy(string fqFileName, string destFileName)
    {
        this.@lock.EnterWriteLock();
        try
        {
            File.Copy(fqFileName, destFileName, true);

            if (this.CopyingFinished == null)
            {
                return;
            }

            this.CopyingFinished(destFileName);
        }
        finally
        {
            this.@lock.ExitWriteLock();
        }
    }
}

public interface IFileWatcher
{
    event Action<string> Changed;
}

public class FileChangeWatcher : IFileWatcher
{
    public FileChangeWatcher(FileSystemWatcher eyes)
    {
        this.Eyes = eyes;
        this.Eyes.Changed += this.OnChangedEvent;
    }

    protected FileSystemWatcher Eyes { get; set; }

    public event Action<string> Changed;

    private void OnChangedEvent(object sender, FileSystemEventArgs eventArgs)
    {
        if (this.Changed == null)
        {
            return;
        }

        this.Changed(eventArgs.FullPath);
    }
}
  • 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-26T15:17:35+00:00Added an answer on May 26, 2026 at 3:17 pm

    What you are doing isn’t correct – when you write a test using only mocks you aren’t really testing anything!

    Instead what you want is to introduce some of your non mocked code, code that uses the objects that you are replacing with mocks.

    Two of the big things that using mocks (and in particular mocking frameworks like moq) gives you is:

    1 – Isolation

    2 – Verifiability

    By isolation I mean that in your case you don’t want to really copy a file every time you test actions on your file copier class. So you replace the real file copier with something that pretends to be the file copier.

    By verifiability I mean that often classes don’t have any way to tell you if they were called. This is usually a good thing since telling other people if they have been called isn’t part of their job, but in testing scenarios you want this information – the expectations in moq allow you to gather it.


    In this case it looks like you have something which raises an event, and then something that listens for this event and calls the copy method on your FileCopier.

    So you want to mock out the event source (or call the concrete source of the event in the right way) and then make that event be raised.

    You then need a second mock – the mock of the FileCopier that has an expectation that its copy method will be called with the correct parameters. This FileCopier mock should be being passed to the class that calls it (Dependency Injection) and this the allows you to confirm that the copy method is called by your real code.

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

Sidebar

Related Questions

I'm very new to Mocking. In the below example i'm using Moq and trying
I'm pretty new to testing and mocking and i am trying to write a
I am new to testing and mocking. I'm trying to test a business logic
new to c#. I'm trying to make a simple system where I can search
I'm trying to use the fluent mocking style of Rhino.Mocks and have the following
I'm trying to write a test for an UrlHelper extensionmethod that is used like
I am trying out the MOQ framework and up now I have hit a
I am trying to learn TDD/BDD using NUnit and Moq. The design that I
I am trying to mock out my Repository with Moq. I am trying to
I am using mock for testing in Python. I am trying to unit test

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.