How to verify a method was invoked with a delegate?

43 views
Skip to first unread message

Dave

unread,
Feb 4, 2012, 1:19:43 PM2/4/12
to rhino...@googlegroups.com
How does one go about testing that a delegate was passed to a method?  I have a class with a method:

class Worker {

    public void MainLoop() {
    }
}

And another class

class Foo {

    public void Bar( Action methodToRunInLoop) {
      ...
    }
}

and some code that does this

Worker w = new Worker();
Foo f = new Foo();
f.Bar(w.MainLoop);

What I want to do is to verify that "f.Bar(w.MainLoop);" was called.

Thanks.

Patrick Steele

unread,
Feb 4, 2012, 10:35:04 PM2/4/12
to rhino...@googlegroups.com
What you should be doing is simply making sure that f.Bar calls the
delegate that is passed to it -- which is really what is happening.
Something like this won't need Rhino.Mocks. You could do:

var methodCalled = false;
var foo = new Foo();
f.Bar(() => methodCalled = true);
Assert.IsTrue(methodCalled);

If bar never calls the delegate passed to it, methodCalled will still
be false and your test will fail.

---
Patrick Steele
http://weblogs.asp.net/psteele

> --
> You received this message because you are subscribed to the Google Groups
> "Rhino.Mocks" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rhinomocks/-/GdRWRi8e948J.
> To post to this group, send email to rhino...@googlegroups.com.
> To unsubscribe from this group, send email to
> rhinomocks+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rhinomocks?hl=en.

Reply all
Reply to author
Forward
0 new messages