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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:43:14+00:00 2026-05-19T15:43:14+00:00

Im using the Prism/Composite Application Library and trying to unit test some code that

  • 0

Im using the Prism/Composite Application Library and trying to unit test some code that subscribes to a CompositePresentationEvent using the EventAggregator. The code that is raising the event is raising it on another thread so I am subscribing to the event using ThreadOption.UIThread.

When the event raises the callback it uses the application dispatcher to put it onto the UI thread. This is fine during normal execution but during a unit test there is no dispatcher. The code in CompositePresentationEvent looks like this:

    private IDispatcherFacade UIDispatcher
    {
        get
        {
            if (uiDispatcher == null)
            {
                this.uiDispatcher = new DefaultDispatcher();
            }

            return uiDispatcher;
        }
    }



public class DefaultDispatcher : IDispatcherFacade
{
    /// <summary>
    /// Forwards the BeginInvoke to the current application's <see cref="Dispatcher"/>.
    /// </summary>
    /// <param name="method">Method to be invoked.</param>
    /// <param name="arg">Arguments to pass to the invoked method.</param>
    public void BeginInvoke(Delegate method, object arg)
    {
        if (Application.Current != null)
        {
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, method, arg);
        }
    }
}

The problem being that CompositePresentationEvent is tied to the DefaultDispatcher, and this dispatcher does nothing if there is no Application running.

Has anyone had any success unit testing in this kind of situation? Any tips or workarounds to kick the dispatcher into life?

I know I could make my callback internal and allow my unit test to call this method but I would prefer not to change my code and leave this approach as a last resort.

  • 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-19T15:43:15+00:00Added an answer on May 19, 2026 at 3:43 pm

    You didn’t post your tests, so I’m unclear what you are trying to test, but most-likely you are trying to test one of the following things:

    1. That the code you are testing subscribes at all
    2. That the code you are testing reacts to events appropriately

    In either case, you are going to want to mock the EventAggregator. Because the Event isn’t what you want to test, but rather the code that utilizes it, you want to provide a fake alternative that does what you want it to do. I’ll try and provide a good example. I use Moq, but you can choose whatever mocking framework you like.

    In this test, I simply assert that Subscribe was called in the constructor, but your test might be more complicated if you are wanting to test the class’s reaction to a raised event. The test shows a CompositePresentationEvent<int>.

    //Arrange
    Mock<MyEvent> mockEvent = new Mock<MyEvent>();
    Mock<IEventAggregator> mockAggregator = new Mock<IEventAggregator>();
    
    mockEvent.Setup
    (
         evnt => evnt.Subscribe(It.IsAny<Action<int>>())
    );
    mockAggregator.Setup
    (
        agg => agg.GetEvent<MyEvent>()
                  .Returns(mockEvent.Object);
    );
    
    //Act
    MyClassIWantToTest target = new MyClassIWantToTest(mockAggregator.Object);
    
    //Assert
    mockEvent.VerifyAll();
    

    That’s the basics. The rule of thumb here is that if you tests rely on a system resource that is difficult to provide, isolate it from the test.

    Edit: after reading your question I see you are trying to test the callback.

    In this sample, I test if the “CurrentValueProperty” property is set to whatever value is passed in the callback method. Here is that sample:

    //Arrange
    Mock<MyEvent> mockEvent = new Mock<MyEvent>();
    Mock<IEventAggregator> mockAggregator = new Mock<IEventAggregator>();
    Action<int> theEventCallback = null;
    
    mockEvent.Setup
    (
        evnt => evnt.Subscribe(It.IsAny<Action<int>>())
    )
    .Callback<Action<int>>
    (
        cb => theEventCallback = cb
    );
    
    
    mockAggregator.Setup
    (
        agg => agg.GetEvent<MyEvent>()
    )
    .Returns(mockEvent.Object);
    
    //Act
    MyClassIWantToTest target = new MyClassIWantToTest(mockAggregator.Object);
    
    //we expect this to be populated by the callback specified in our mock setup
    //that will be triggered when Subscribe is called in 
    //MyClassIWantToTest's constructor
    theEventCallback(27);
    
    //Assert
    Assert.AreEqual(target.CurrentValueProperty, 27);
    

    That’s it.

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

Sidebar

Related Questions

I am currently working on a project of mine using Prism (the Composite Application
I am building a composite application using CAL/Prism. The main region is a tab
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
We were building out the next version of an in-house thick-client application using WPF/Prism
I'm working on a Prism Composite application where I load different views into a
In a Composite Application (Prism), when my module loads , I get this error:
Im building a wpf app with the composite application block (prism) V2, and Im
We are currently developing Sliverlight 4.0 based web application using PRISM 4.0 for Sliverlight
I am writing an WPF MVVM application using Prism. A couple days ago I
I'm looking at using CompositeWPF ( http://www.codeplex.com/CompositeWPF ) - aka Prism, to build an

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.