起订量事件聚合器是否可能

发布于 2024-09-18 13:50:03 字数 2608 浏览 6 评论 0原文

想知道是否可以最小起订量 Prism EventAggregator 让我们以 EventAggregator 快速入门为例,他们

     [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 

有 MockEventAggregator。我如何使用 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);
    }
}

有人使用过 MOQ 和 EventAggregator 吗?有什么例子吗? 非常感谢

编辑

遵循 GrameF 答案我添加了我的代码但仍然不起作用。你能帮忙吗

            [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);
            }

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);
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗失的美好 2024-09-25 13:50:03

是的,这是可能的,您只需将其设置为返回一个模拟事件,您可以在该事件上验证是否调用了 PublishSubscribe

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

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>()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文