开发者

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 MockFundAd开发者_StackOverflowdedEvent();
         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);
            }


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>()));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜