起订量 - mock.Raise应提高测试单元事件而无需安装 [英] Moq - mock.Raise should raise event in tested unit without having a Setup

查看:238
本文介绍了起订量 - mock.Raise应提高测试单元事件而无需安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主持人类,附加注入的视图的事件。
现在我想测试演示正确地对事件的反应上。

I have a presenter class, that attaches an event of the injected view. Now I would like to test the presenter reacting on correctly on the event.

这是视图接口的iView:

This is the view interface IView:

public interface IView 
{
    event EventHandler MyEvent;
    void UpdateView(string test);
}

这北京时间实施的iView

This ist the view implementing IView

public partial class MyView : IView
{
    public event EventHandler MyEvent;

    public MyView()
    {
        this.combo.SelectedIndexChanged += this.OnSelectedIndexChanged;
    }

    public void UpdateView(string test)
    {
        this.textBox.Text = test;
    }

    private OnSelectedIndexChanged(Object sender, EventArgs e)
    {
        if (this.MyEvent != null)
        {
            this.MyEvent(sender, e);
        }
    }
}

这北京时间测试演示

public class MyPresenter
{
    private IView _view;
    public MyPresenter(IView view)
    {
        this._view = view;
        this._view.MyEvent += this.OnMyEvent;
    }

    private void OnMyEvent(Object sender, EventArgs e)
    {
        this._view.UpdateView();
    }
}

这是测试夹具测试MyPresenter:

This is the test fixture testing MyPresenter:

[TestClass]
public class MyPresenterFixture()
{
    private MyPresenter testee;
    private Mock<IView> mockView;

    [TestMethod]
    public void ShouldReactOnMyEvent()
    {
        // arrange
        this.mockView = new Mock<IView>(MockBehavior.Strict);
        this.testee = new MyPresenter(this.mockView.Object);

        // act
        this.mockView.Raise(mock => mock.MyEvent += null); // this does not fire

        // assert and verify
        this.mockView.Verify(mock => mock.UpdateView(It.IsAny<string>());
    }
}

我用的起订量4.是否可以做我想做的?

I am using Moq 4. Is it possible to do what I want?

最好的问候
Yannik

Best regards Yannik

推荐答案

别ŧ你需要传递参数?你的活动签名EvenHandler,这是对象,EventArgs的。

Don't you need to pass the argument? Your event signature is EvenHandler, which is object, EventArgs.

this.mockView.Raise(mock => mock.MyEvent += null, new EventArgs());

我从来没有使用你在这里指定的超负荷......它似乎并不正确,虽然。

I've never used the overload you've specified here... it doesn't seem correct, though.

这篇关于起订量 - mock.Raise应提高测试单元事件而无需安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆