Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

prism - Moq Event Aggregator Is it possible

Wondering if its possible to Moq the Prism EventAggregator Let's take the EventAggregator Quickstart they have

     [TestMethod]
      public void PresenterPublishesFundAddedOnViewAddClick()
      {
         var view = new MockAddFundView();
         var EventAggregator = new MockEventAggregator();
         var mockFundAddedEvent = new MockFundAddedEvent();
         EventAggregator.AddMapping<FundAddedEvent>(mockFundAddedEvent);
         var presenter = new AddFundPresenter(EventAggregator);
         presenter.View = view;
         view.Customer = "99";
         view.Fund = "TestFund";
         view.PublishAddClick();
         Assert.IsTrue(mockFundAddedEvent.PublishCalled);
         Assert.AreEqual("99", mockFundAddedEvent.PublishArgumentPayload.CustomerId);
      }       

    I have tried to convert the above using moq but I get problems 

they have MockEventAggregator.How can I do that using Moq?

public class MockEventAggregator : IEventAggregator
{
    Dictionary<Type, object> events = new Dictionary<Type, object>();
    public TEventType GetEvent<TEventType>() where TEventType : EventBase
    {
        return (TEventType)events[typeof(TEventType)];
    }

    public void AddMapping<TEventType>(TEventType mockEvent)
    {
        events.Add(typeof(TEventType), mockEvent);
    }
}

Has anybody used MOQ and the EventAggregator are there any examples out there? Thanks a lot

EDIT

Following GrameF Answer I have added my code that still does not work.Can you help

            [TestMethod]
            public void PresenterPublishesFundAddedOnViewAddClick2()
            {

                //Arrange
                var view = new Mock<IAddFundView>();
                var fakeEventAggregator = new Mock<IEventAggregator>();
                var fakeMyEvent = new Mock<FundAddedEvent>();
                fakeEventAggregator.Setup(x => x.GetEvent<FundAddedEvent>()).Returns(fakeMyEvent.Object); 

                var presenter = new AddFundPresenter(fakeEventAggregator.Object) {View = view.Object};
                fakeMyEvent.Verify(x => x.Publish(It.IsAny<FundOrder>())); **//CRASHES** HERE



                //view.PublishAddClick();
                //view.Customer = "99";
                //view.Fund = "TestFund";

                //view.PublishAddClick();

                ////Assert 
                //Assert.IsTrue(mockFundAddedEvent.PublishCalled);
                //Assert.AreEqual("99", mockFundAddedEvent.PublishArgumentPayload.CustomerId);
                //Assert.AreEqual("TestFund", mockFundAddedEvent.PublishArgumentPayload.TickerSymbol);
            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, it's possible, you just need to set it up to return a mock event on which you can verify that Publish or Subscribe was called:

var fakeEventAggregator = new Mock<IEventAggregator>();
var fakeMyEvent = new Mock<MyEvent>();

fakeEventAggregator.
    Setup(x => x.GetEvent<MyEvent>()).
    Returns(fakeMyEvent.Object);

var test = new Foo(fakeEventAggregator.Object);
test.PublishAnEvent();

fakeMyEvent.Verify(x => x.Publish(It.IsAny<MyEventArgs>()));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...