开发者

How do I set the expectation of an event when calling a method?

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 clas开发者_运维问答s.

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?


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);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜