使用 Rhino 从模拟对象引发事件
我在模拟对象上引发事件时遇到问题。我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为是这样的:
http://ayende.com/wiki/Rhino+Mocks +3.5.ashx#Howtoraiseevents
Something like this I think:
http://ayende.com/wiki/Rhino+Mocks+3.5.ashx#Howtoraiseevents
您需要一个
IEventRaiser
,您可以通过以下方式获取它然后,当您想要引发事件时,您可以使用所需的参数(例如发送者和事件参数)调用
eventRaiser.Raise
(取决于您的事件处理程序定义)。(编辑:这是基于Rhino.Mocks 3.1!)
You need an
IEventRaiser
, which you can get viaThen, 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!)