CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

EventAggregator with MEF syntax

482 Views
Copy Code Show/Hide Line Numbers
[TestClass]
public class MefEventAggregatorTestFixture
{
    [TestMethod]
    public void Subscriber_is_invoked_when_publisher_raises_event()
    {
       var catalog = new InterceptingCatalog(
            new TypeCatalog(typeof(EventAggregator), typeof (FakePublisher), typeof (MockSubscriber))
            );
        var container = new CompositionContainer(catalog);
        var publisher = container.GetExportedValue<FakePublisher>();
        var subscriber = container.GetExportedValue<MockSubscriber>();
        publisher.RaiseTestEvent();
        Assert.IsTrue(subscriber.ExecuteWasCalled);
    }
 
}
 
[Export]
public class FakePublisher
{
    private EventAggregator _ea;
 
    [ImportingConstructor]
    public FakePublisher(EventAggregator ea)
    {
        _ea = ea; 
    }
 
    public void RaiseTestEvent()
    {
        _ea.Publish(new FakeMessage());
    }
}
 
[Export]
public class MockSubscriber
{
    public bool ExecuteWasCalled { get; set; }
 
    [EventSubscription]
    public void Execute(FakeMessage message)
    {
        ExecuteWasCalled = true;
    }        
}
 
public class FakeMessage { }
by Glenn Block
  September 01, 2009 @ 6:22pm
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate