使用 Rhino 从模拟对象引发事件

发布于 2024-11-07 23:27:31 字数 1338 浏览 0 评论 0原文

我在模拟对象上引发事件时遇到问题。我正在使用 Rhino Mocks 3.4。我研究过类似的问题,但未能重现任何建议的解决方案。

我有一个类 - Foo - 它有一个私有方法,只能通过注入接口 - IBar 的事件调用来访问。

如何从 RhinoMock 对象引发事件 IBar.BarEvent,以便我可以在 Foo 中测试该方法?

这是我的代码:

[TestFixture]
public sealed class TestEventRaisingFromRhinoMocks
{

    [Test]
    public void Test()
    {
        MockRepository mockRepository = new MockRepository();
        IBar bar = mockRepository.Stub<IBar>();

        mockRepository.ReplayAll();

        Foo foo = new Foo(bar);

        //What to do, if I want invoke bar.BarEvent with value =123??

        Assert.That(foo.BarValue, Is.EqualTo(123));

    }

}


public class Foo
{
    private readonly IBar _bar;
    private int _barValue; 

    public Foo(IBar bar)
    {
        _bar = bar;
        _bar.BarEvent += BarHandling; 
    }

    public int BarValue
    {
        get { return _barValue; }
    }

    private void BarHandling(object sender, BarEventArgs args)
    {
        //Eventhandling here: How do I get here with a Rhino Mock object? 
        _barValue = args.BarValue;
    }
}


public interface IBar
{
    event EventHandler<BarEventArgs> BarEvent;
}


public class BarEventArgs:EventArgs
{
    public BarEventArgs(int barValue)
    {
        BarValue = barValue;
    }
    public int BarValue { get; set; } 
}

I have a problem with raising an event on a mocked object. I am using Rhino Mocks 3.4. I have studied similar questions, but failed to reproduce any of the suggested solutions.

I have a class -- Foo -- which have a private method, that is only accessed by event invokation by an injected interface -- IBar.

How do I raise the event IBar.BarEvent, from a RhinoMock object, so I can Test the method in Foo?

Here is my code:

[TestFixture]
public sealed class TestEventRaisingFromRhinoMocks
{

    [Test]
    public void Test()
    {
        MockRepository mockRepository = new MockRepository();
        IBar bar = mockRepository.Stub<IBar>();

        mockRepository.ReplayAll();

        Foo foo = new Foo(bar);

        //What to do, if I want invoke bar.BarEvent with value =123??

        Assert.That(foo.BarValue, Is.EqualTo(123));

    }

}


public class Foo
{
    private readonly IBar _bar;
    private int _barValue; 

    public Foo(IBar bar)
    {
        _bar = bar;
        _bar.BarEvent += BarHandling; 
    }

    public int BarValue
    {
        get { return _barValue; }
    }

    private void BarHandling(object sender, BarEventArgs args)
    {
        //Eventhandling here: How do I get here with a Rhino Mock object? 
        _barValue = args.BarValue;
    }
}


public interface IBar
{
    event EventHandler<BarEventArgs> BarEvent;
}


public class BarEventArgs:EventArgs
{
    public BarEventArgs(int barValue)
    {
        BarValue = barValue;
    }
    public int BarValue { get; set; } 
}

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

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

发布评论

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

评论(2

神经大条 2024-11-14 23:27:31

我认为是这样的:

bar.Raise(x => x.BarEvent += null, this, EventArgs.Empty);

http://ayende.com/wiki/Rhino+Mocks +3.5.ashx#Howtoraiseevents

Something like this I think:

bar.Raise(x => x.BarEvent += null, this, EventArgs.Empty);

http://ayende.com/wiki/Rhino+Mocks+3.5.ashx#Howtoraiseevents

╰ゝ天使的微笑 2024-11-14 23:27:31

您需要一个IEventRaiser,您可以通过以下方式获取它

bar.BarEvent += null;
var eventRaiser = LastCall.IgnoreArguments().GetEventRaiser();

然后,当您想要引发事件时,您可以使用所需的参数(例如发送者和事件参数)调用eventRaiser.Raise (取决于您的事件处理程序定义)。

(编辑:这是基于Rhino.Mocks 3.1!)

You need an IEventRaiser, which you can get via

bar.BarEvent += null;
var eventRaiser = LastCall.IgnoreArguments().GetEventRaiser();

Then, when you want to raise the event, you can call eventRaiser.Raise with the required arguments, e.g. sender and event args (depends on your event handler definition).

(Edit: this is based on Rhino.Mocks 3.1!)

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