gpt4 book ai didi

moq - Moq Event Aggregator是否可能

转载 作者:行者123 更新时间:2023-12-04 16:41:16 26 4
gpt4 key购买 nike

想知道是否有可能将 Prism 事件聚合器最小化
让我们以他们拥有的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);
}
}

有没有人使用最小起订量和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);
}

最佳答案

是的,有可能,您只需对其进行设置即可返回一个模拟事件,在该事件上您可以验证是否调用了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>()));

关于moq - Moq Event Aggregator是否可能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3694195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com