views:

24

answers:

1

I've implemented an ITimer interface because I want to write some tests around a class I'm building that utilizes the System.Timers.Timer class.

So the sequence goes when I call Timer.Start() some time later I expect the Elapsed event to occur.

However, for my test I want to mock out this behavior, because I don't want to wait a certain amount of time for the event to occur (i want it to happen immediately).

I was trying to mock out the Start method and have it simply raise the event, but I don't know how to accomplish that.

This is kind of what I have so far:

var _mock = MockRepository.GenerateMock<ITimer>();
//this is what I'd like to do but it doesn't work
Expect
    .Call(_mock.Start())
    .WhenCalled( () =>
        {
            if (_mock.Elapsed != null)
                _mock.Elapsed();
        });

The problem I'm getting is the compiler is telling me that the event can only be used in the context of a += or a -= operation.

There has to be a way to do this using Rhino Mocks, does anyone know how to do this?

+2  A: 

Turns out I was looking in the wrong place. I found the answer here.

What I needed to do was something along these lines:

var _mock = MockRepository.GenerateMock<ITimer>();
_mock.Raise(x => x.Elapsed += null, this, EventArgs.Empty);
Joseph