如何在 Rhino Mocks 中的严格模拟中引发事件?
我想测试以下代码在引发 MyEvent 事件时是否设置 Raising 属性:
public interface IEventRaiser
{
event EventHandler<Args> MyEvent;
void DoSomething();
}
public class EventSubscriber
{
public EventSubscriber(IEventRaiser raiser)
{
this.raiser = raiser;
this.raiser.MyEvent += EventRaised;
}
private readonly IEventRaiser raiser;
private void EventRaised(object sender, Args args)
{
raiser.DoSomething();
Raised = true;
}
public bool Raised {get; private set;}
}
我正在使用以下 NUnit 测试代码,但该事件似乎从未引发。 EventRaished 方法永远不会运行:
[TestFixture]
public class EventSubscriberTests
{
private MockRepository mocks;
private IEventRaiser raiser;
private EventSubscriber target;
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
raiser = mocks.StrictMock<IEventRaiser>();
raiser.Expect(r => r.MyEvent += null).IgnoreArguments();
mocks.ReplayAll();
target = new EventSubscriber(raiser);
}
[TearDown]
public void TearDown()
{
mocks.VerifyAll();
}
[Test]
public void VerifyEventIsSubscribedTo()
{
raiser.BackToRecord();
raiser.Expect(r => r.DoSomething());
raiser.Replay();
raiser.Raise(r => r.MyEvent += null, raiser, new Args {Property = true} );
Assert.IsTrue(target.Raised);
}
}
如果我在引发事件之前不添加额外的期望,则效果很好。但是,我似乎无法让它与它一起工作。我做错了什么?
I want to test that the following code sets the Raised property when the MyEvent event is raised:
public interface IEventRaiser
{
event EventHandler<Args> MyEvent;
void DoSomething();
}
public class EventSubscriber
{
public EventSubscriber(IEventRaiser raiser)
{
this.raiser = raiser;
this.raiser.MyEvent += EventRaised;
}
private readonly IEventRaiser raiser;
private void EventRaised(object sender, Args args)
{
raiser.DoSomething();
Raised = true;
}
public bool Raised {get; private set;}
}
I am using the following NUnit test code, but the event never seems to be raised. The EventRaised method is never running:
[TestFixture]
public class EventSubscriberTests
{
private MockRepository mocks;
private IEventRaiser raiser;
private EventSubscriber target;
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
raiser = mocks.StrictMock<IEventRaiser>();
raiser.Expect(r => r.MyEvent += null).IgnoreArguments();
mocks.ReplayAll();
target = new EventSubscriber(raiser);
}
[TearDown]
public void TearDown()
{
mocks.VerifyAll();
}
[Test]
public void VerifyEventIsSubscribedTo()
{
raiser.BackToRecord();
raiser.Expect(r => r.DoSomething());
raiser.Replay();
raiser.Raise(r => r.MyEvent += null, raiser, new Args {Property = true} );
Assert.IsTrue(target.Raised);
}
}
If I don't add the extra expectation in before raising the event, this works fine. However, I can't seem to make it work with it. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BackToRecord 方法默认清除所有先前的期望。我实际上需要使用 BackToRecord(BackToRecordOptions.None) 来保持以前的状态。
The BackToRecord method clears all previous expectations by default. I actually needed to use BackToRecord(BackToRecordOptions.None) in order to maintain the previous state.