如何验证在使用 Prism 处理 ViewModel 时调用 EventAggregator 的取消订阅方法

发布于 2024-12-08 05:41:48 字数 1475 浏览 0 评论 0原文

我正在努力编写一个测试,以确认我在 EventAggregator 关闭时正确取消订阅它的消息。有人能指出(简单)答案吗?!

这是代码:

public class ViewModel : BaseViewModel, IViewModel
{
    private readonly IEventAggregator eventAggregator;
    private SubscriptionToken token;
    IssuerSelectedEvent issuerSelectedEvent;

    public ViewModel(IView view, IEventAggregator eventAggregator)
    {
        this.eventAggregator = eventAggregator;
        View = view;        

        issuerSelectedEvent = eventAggregator.GetEvent<IssuerSelectedEvent>();
        token = issuerSelectedEvent.Subscribe(SelectedIssuerChanged, true);            
    }

    private void SelectedIssuerChanged(IssuerSelectedCommand obj)
    {
        Console.WriteLine(obj);
    }

    public IView View { get; set; }

    public override void Dispose()
    {
        issuerSelectedEvent.Unsubscribe(token);
    }
}

测试失败并显示:

Moq.MockVerificationException:以下设置不匹配: IssuerSelectedEvent x =>; x.Unsubscribe(It.IsAny())

这是测试:

[Test]
public void UnsubscribeFromEventAggregatorOnDispose()
{            
    var view = new Mock<ICdsView>();
    var ea = new Mock<EventAggregator>();
    var evnt = new Mock<IssuerSelectedEvent>();

    evnt.Setup(x => x.Unsubscribe(It.IsAny<SubscriptionToken>()));

    var vm = new CdsIssuerScreenViewModel(view.Object, ea.Object);

    vm.Dispose();

    evnt.VerifyAll();
}

I'm struggling to write a test that confirms that I am correctly unsubscribing from an EventAggregator's message when it is closed. Anyone able to point out the (simple) answer?!

Here is the code:

public class ViewModel : BaseViewModel, IViewModel
{
    private readonly IEventAggregator eventAggregator;
    private SubscriptionToken token;
    IssuerSelectedEvent issuerSelectedEvent;

    public ViewModel(IView view, IEventAggregator eventAggregator)
    {
        this.eventAggregator = eventAggregator;
        View = view;        

        issuerSelectedEvent = eventAggregator.GetEvent<IssuerSelectedEvent>();
        token = issuerSelectedEvent.Subscribe(SelectedIssuerChanged, true);            
    }

    private void SelectedIssuerChanged(IssuerSelectedCommand obj)
    {
        Console.WriteLine(obj);
    }

    public IView View { get; set; }

    public override void Dispose()
    {
        issuerSelectedEvent.Unsubscribe(token);
    }
}

The test fails with:

Moq.MockVerificationException : The following setups were not matched:
IssuerSelectedEvent x => x.Unsubscribe(It.IsAny())

Here is the test:

[Test]
public void UnsubscribeFromEventAggregatorOnDispose()
{            
    var view = new Mock<ICdsView>();
    var ea = new Mock<EventAggregator>();
    var evnt = new Mock<IssuerSelectedEvent>();

    evnt.Setup(x => x.Unsubscribe(It.IsAny<SubscriptionToken>()));

    var vm = new CdsIssuerScreenViewModel(view.Object, ea.Object);

    vm.Dispose();

    evnt.VerifyAll();
}

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

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

发布评论

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

评论(3

巾帼英雄 2024-12-15 05:41:48

在这里,我验证是否在模拟的 IssuerSelectedEvent 上调用了 Unsubscribe

[Test]
public void UnsubscribeFromEventAggregatorOnDispose()
{            
    var view = new Mock<ICdsView>();
    var ea = new Mock<IEventAggregator>();
    var evnt = new Mock<IssuerSelectedEvent>();

    ea.Setup(x => x.GetEvent<IssuerSelectedEvent>()).Returns(evnt.Object);

    var vm = new CdsIssuerScreenViewModel(view.Object, ea.Object);

    vm.Dispose();

    evnt.Verify(x => x.Unsubscribe(It.IsAny<SubscriptionToken>());
}

如果您想检查是否将完全相同的令牌传递到 Unsubscribe 中,则您将需要 Subscribe 方法的 Setup 来返回您在测试中创建的令牌。

Here I am verifying that the Unsubscribe was called on the mocked IssuerSelectedEvent

[Test]
public void UnsubscribeFromEventAggregatorOnDispose()
{            
    var view = new Mock<ICdsView>();
    var ea = new Mock<IEventAggregator>();
    var evnt = new Mock<IssuerSelectedEvent>();

    ea.Setup(x => x.GetEvent<IssuerSelectedEvent>()).Returns(evnt.Object);

    var vm = new CdsIssuerScreenViewModel(view.Object, ea.Object);

    vm.Dispose();

    evnt.Verify(x => x.Unsubscribe(It.IsAny<SubscriptionToken>());
}

If you want to check that the exact same token is passed into the Unsubscribe then you will need a Setup for the Subscribe method that returns a token you create in your test.

旧人 2024-12-15 05:41:48

您需要告诉您的 EventAggregator 模拟返回您的模拟 IssuerSelectedEvent

ea.Setup(x => x.GetEvent<IssuerSelectedEvent>()).Return(evnt.Object);

You need to tell your EventAggregator mock to return your mocked IssuerSelectedEvent:

ea.Setup(x => x.GetEvent<IssuerSelectedEvent>()).Return(evnt.Object);
傲性难收 2024-12-15 05:41:48

测试需要更改为:

[Test]
public void UnsubscribeFromEventAggregatorOnDispose()
{            
    var view = new Mock<ICdsView>();
    var ea = new Mock<IEventAggregator>();
    var evnt = new Mock<IssuerSelectedEvent>();

    ea.Setup(x => x.GetEvent<IssuerSelectedEvent>()).Returns(evnt.Object);
    evnt.Setup(x => x.Unsubscribe(It.IsAny<SubscriptionToken>()));

    var vm = new CdsIssuerScreenViewModel(view.Object, ea.Object);

    vm.Dispose();

    evnt.VerifyAll();
}

The tests needs to be changed to:

[Test]
public void UnsubscribeFromEventAggregatorOnDispose()
{            
    var view = new Mock<ICdsView>();
    var ea = new Mock<IEventAggregator>();
    var evnt = new Mock<IssuerSelectedEvent>();

    ea.Setup(x => x.GetEvent<IssuerSelectedEvent>()).Returns(evnt.Object);
    evnt.Setup(x => x.Unsubscribe(It.IsAny<SubscriptionToken>()));

    var vm = new CdsIssuerScreenViewModel(view.Object, ea.Object);

    vm.Dispose();

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