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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:25:43+00:00 2026-06-12T03:25:43+00:00

Is there a way to unit test raising an event with moq if an

  • 0

Is there a way to unit test raising an event with moq if an event that is used in an interface implementation does not appear in the interface you are mocking?

Note: My interface doesn’t have anything to do with my UI and my events are just used for UI notifications, so I wanted to decouple that behavior from the actual interface as the repository is in a separate library from the client/UI.

For example:

    [Test]
    public void TestRaiseBarProcessed()
    {
        ManualResetEvent barProcessedEvent = new ManualResetEvent(false);
        bool called = false;

        //Arrange
        Mock<IFooRepository> mockFooRepository = new Mock<IFooRepository>();

        mockSourceRepository
            .Setup(a => a.SearchForBar(barsToFind))
            .Returns(barsFound)
            .Raises(
                a => a.BarProcessed += null, 
                new BarFoundEventArgs(It.IsAny<string>()));

        IList<IFooRepository> mockFooRepositories = 
            new List<IFooRepository>();

        mockFooRepositories.Add(mockFooRepository.Object);

        FooBar fooBar = new FooBar(mockFooRepositories, FooList);

        fooBar.CurrentBarBeingProcessedInfo += (sender, e) =>
            {
                barProcessedEvent.Set();
                called = true;
            };

        //Act
        fooBar.CallFooRepositoryMethod();

        barProcessedEvent.WaitOne(25, false);

        //Assert
        mockFooRepository.Verify(
            a => a.SearchForBar(barsToFind),
            Times.Once());

        Assert.AreEqual(true, called);
    }

Let me know if any of this needs more clarification.

  • 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-12T03:25:45+00:00Added an answer on June 12, 2026 at 3:25 am

    Did you consider having that UI-only event defined in another interface and implementing both of them? If so, you could then use Moq’s multiple-interface mocks:

    var fooRepositoryMock = new Mock<IFooRepository>();
    var barUiEventMock = fooRepositoryMock.As<IBarUiEvent>();
    bool wasCalled = false;
    barUiEventMock.Object.BarProcessed += (s, e) => wasCalled = true;
    
    fooRepositoryMock
        .Setup(m => m.SearchForBars(barsToFind))
        .Returns(barsFound)
        .Raises(
            foo => barUiEventMock.Object.BarProcessed += null,
            new BarFoundEventArgs("")
        );
    
    // ...
    
    Assert.That(wasCalled, Is.True);
    

    Call to SearchForBars will cause BarProcessed event to be raised with whatever arguments you pass.

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

Sidebar

Related Questions

Is there a way to do unit test for module which does not return
Is there an easy way to check in a unit test that two arrays
Is there a proper way yet to unit test views with the aspx view
How to unit test JPA code? is there any way to generate Unit Test
Using Visual Studio Test Suite, is there a way to make a single unit
In the Python unittest framework, is there a way to pass a unit test
Is there a way to automate the saving of the unit test results in
Duplicate: Unit Testing the Views? Is there any way to unit test View? I
Is there a way to write unit tests so that they can be compiled
Is there a way to implement a parameterized unit test with Scala ? Currently

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.