How to raise an event from a method call, and pass any method param to event arg?

1,457 views
Skip to first unread message

dan

unread,
Jan 27, 2009, 1:56:52 AM1/27/09
to Moq Discussions
Hi

There is eventarg defined like this:

public DR1000EventArgs(object newValue, object oldValue);

my interface defines an event and method like this:

event EventHandler<DR1000EventArgs> KvpChanged;
void SetKvp(int value);

I want to set up the mock to raise the KvpChanged event (with w/ the
method param passed in as the first parameter of the
DR1000EventArgs).

ie if I call SetKvp(100) I want the KvpChanged event to raise w/
DR1000EventArgs(100, whatever);

the quick start says this:

// Causing an event to raise automatically when Submit is invoked
mock.Expect(foo => foo.Submit()).Raises(f => f.Sent += null,
EventArgs.Empty);

I am using Moq 3.0.108.5... so I tried

mock.Setup(foo => foo.SetKvp(It.IsAny<int>())).Raises(f => f.KvpChange
+= null, new DR1000EventArgs(It.IsAny<int>(), It.IsAny<int>()));

but it doesn't like the Raises part - there is no intellisense for the
f => f.??????

thanks

dan

Daniel Cazzulino

unread,
Jan 27, 2009, 4:39:35 AM1/27/09
to moq...@googlegroups.com

Will have to review this. Don't remember if the event syntax is supported already in Raises in the beta...

Thanks for reporting and trying out 3.0b !

/kzu from android mobile

andreister

unread,
Jan 27, 2009, 2:29:02 PM1/27/09
to Moq Discussions
Try Callback:

mock.Setup(foo => foo.SetKvp(It.IsAny<int>()))
.Callback(() => mock.Raise(x => x.KvpChange += null, new
DR1000EventArgs()));
//.Returns(...); - you can add Retuns if the call should return
anything

Daniel Cazzulino

unread,
Feb 3, 2009, 1:28:18 PM2/3/09
to moq...@googlegroups.com
I've just committed and uploaded a new release that implements this scenario :)
Please take a look and let me know how/if it works for you.

Sample:

mock.Setup(m => m.Submit("foo"))
        .Raises(
            m => m.Submitted += null,
            (string msg) => new SubmitEventArgs(msg));
--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471

Daniel Cazzulino

unread,
Feb 3, 2009, 1:41:16 PM2/3/09
to moq...@googlegroups.com
ok, it's in the trunk
still about to upload (will let you know when it's up)

Daniel Cazzulino

unread,
Feb 3, 2009, 4:20:54 PM2/3/09
to moq...@googlegroups.com
deployed!

http://moq.me/get
Reply all
Reply to author
Forward
0 new messages