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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:41:32+00:00 2026-05-15T05:41:32+00:00

In my unit test how can I verify that an event is raised by

  • 0

In my unit test how can I verify that an event is raised by the mocked object.

I have a View(UI) –> ViewModel –> DataProvider –> ServiceProxy. ServiceProxy makes async call to serivce operation. When async operation is complete a method on DataProvider is called (callback method is passed as a method parameter). The callback method then raise and event which ViewModel is listening to.

For ViewModel test I mock DataProvider and verify that handler exists for event raised by DataProvider. When testing DataProvider I mock ServiceProxy, but how can I test that callback method is called and event is raised.

I am using RhinoMock 3.5 and AAA syntax

Thanks

— DataProvider —

public partial class DataProvider
{
    public event EventHandler<EntityEventArgs<ProductDefinition>> GetProductDefinitionCompleted;

    public void GetProductDefinition()
    {
        var service = IoC.Resolve<IServiceProxy>();
        service.GetProductDefinitionAsync(GetProductDefinitionAsyncCallback);
    }

    private void GetProductDefinitionAsyncCallback(ProductDefinition productDefinition, ServiceError error)
    {
        OnGetProductDefinitionCompleted(this, new EntityEventArgs<ProductDefinition>(productDefinition, error));
    }

    protected void OnGetProductDefinitionCompleted(object sender, EntityEventArgs<ProductDefinition> e)
    {
        if (GetProductDefinitionCompleted != null)
            GetProductDefinitionCompleted(sender, e);
    }
}

— ServiceProxy —

public class ServiceProxy : ClientBase<IService>, IServiceProxy
{
    public void GetProductDefinitionAsync(Action<ProductDefinition, ServiceError> callback)
    {
        Channel.BeginGetProductDefinition(EndGetProductDefinition, callback);
    }

    private void EndGetProductDefinition(IAsyncResult result)
    {
        Action<ProductDefinition, ServiceError> callback =
            result.AsyncState as Action<ProductDefinition, ServiceError>;

        ServiceError error;
        ProductDefinition results = Channel.EndGetProductDefinition(out error, result);

        if (callback != null)
            callback(results, error);
    }
}
  • 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-15T05:41:33+00:00Added an answer on May 15, 2026 at 5:41 am

    It sounds like you have two different unit tests to write:

    1. Service Proxy unit test: This test will make sure that the callback sent in to the ServiceProxy will be called upon completion of the async call.

    2. Data Provider unit test: This test will make sure that when a certain method is called, an event is raised (assuming there were some subscribers).

    Which one are you looking for help on?

    EDIT:

    For item #1, I don’t see that you’d need any mocking. Simply provide a callback that sets some variable to true when called:

    // arrange
    IServiceProxy serviceProxy = new ServiceProxy();
    bool callbackMade;
    
    // act
    serviceProxy.GetDataAsync(() => callbackMade = true);
    
    // assert
    Assert.IsTrue(callbackMade);
    

    For item #2, again, just subscribe to the event in your unit test and make sure the event is called:

    // arrange
    DataProvider dp = new DataProvider();
    bool eventRaised;
    dp.DataReturned += (s,e) => eventRaised = true;
    
    // act
    dp.DoSomethingThatShouldRaiseEvent();
    
    // assert
    Assert.IsTrue(eventRaised)
    

    I don’t know the signatures of your events/callbacks so I just made some guesses.

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

Sidebar

Related Questions

Can someone tell me why this unit test that checks for exceptions fails? Obviously
As topic suggested, how can I unit test a function that does not return
I'm trying to write a simple unit test that will verify that, under a
I'm writing a unit test to verify that the serializable attributes are set on
How can I unit test a method which uses a session object inside of
How can I unit test my NSURLConnection delegate? I made a ConnectionDelegate class which
I can typically test a regular Test::Unit method using the following commandline syntax for
How can I write a unit test to test the ActualWidth property in a
Can I use FlexUnit to unit test a File Upload and delete? Are those
Can you share the way you setup your unit test projects within your .net

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.