开发者

How do you raise an event from a Strict Mock in Rhino Mocks?

开发者 https://www.devze.com 2023-02-19 11:49 出处:网络
I want to test that the following code sets the Raised property when the MyEvent event is raised: public interface IEventRaiser

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;
开发者_如何学C    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?


The BackToRecord method clears all previous expectations by default. I actually needed to use BackToRecord(BackToRecordOptions.None) in order to maintain the previous state.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号