Mocking Events

141 views
Skip to first unread message

Chaks

unread,
Mar 20, 2008, 9:29:11 AM3/20/08
to Moq Discussions
Hi All,

I have something like this,

#region Friends
public void GetFriends()
{
provider.UserEventHandler += new
EventHandler<UserEventArgs>(GetFriendsMethodCompleted);

provider.RetrieveFriends();
}

private void GetFriendsMethodCompleted(object sender, UserEventArgs e)
{
OnRaiseFriendsEventCompleted(e.Users);
}

private void OnRaiseFriendsEventCompleted(IEnumerable<User> friends)
{
if (userEventHandler != null)
{
UserEventArgs e = new UserEventArgs(friends);

userEventHandler(this, e);
}
}
#endregion

And my *provider* is the Interface which I want to mock and it has got
some events. And this code registers for an event and once I encounter
that event, I raise another event called
*OnRaiseFriendsEventCompleted*. So first of all I want to test whether
my *provider.RetrieveFriends()* raises the event and check the
*UserEventArgs*. After that another separate test to raise the
*userEventHandler*

Is this possible How do I go about it? (I had to write this code
before doing writing the test)

Thanks,

Regards,
Chaks

tgmdbm

unread,
Mar 20, 2008, 10:49:57 AM3/20/08
to Moq Discussions
Two things...

1) If you're testing the provider you DON'T want to mock it. You'll
want to mock the things which IT uses. If on the other hand you're
testing your "Friends" code then yeah, mock the provider but you might
not need to mock the events. Just call the methods directly.

providerMock.Expect( p => p.RetrieveFriends() )

Friends target = new Friends(providerMock.Object)
target.GetFriends()
target.GetFriendsMethodComplete(null, new UserEventArgs(...))


2) I think you should be able to mock Events.

Just like properties are getting their own ExpectGet and ExpectSet
methods on the Mock<> object, it might be appropriate to add
ExpectAdd, ExpectRemove, RaiseEvent for event handlers?? RaiseEvent
would call all the added handlers.

providerMock.ExpectAdd( p => p.UserEventHandler ) <-- this captures
the added handler
providerMock.Expect( p => p.RetrieveFriends() )

Friends target = new Friends(providerMock.Object)
target.GetFriends()
providerMock.RaiseEvent( p => p.UserEventHandler, null, new
UserEventArgs(...) ) <-- this calls the captured handlers.

Chaks

unread,
Mar 20, 2008, 10:57:38 PM3/20/08
to Moq Discussions
Hi,

Thanks for the reply :)

On Mar 21, 3:49 am, tgmdbm <james....@gmail.com> wrote:
> Two things...
> providerMock.Expect( p => p.RetrieveFriends() )
>
> Friends target = new Friends(providerMock.Object)
> target.GetFriends()
> target.GetFriendsMethodComplete(null, new UserEventArgs(...))
>

Thanks for that. Will see how it goes :)

> 2) I think you should be able to mock Events.
>
> Just like properties are getting their own ExpectGet and ExpectSet
> methods on the Mock<> object, it might be appropriate to add
> ExpectAdd, ExpectRemove, RaiseEvent for event handlers?? RaiseEvent
> would call all the added handlers.
>
> providerMock.ExpectAdd( p => p.UserEventHandler ) <-- this captures
> the added handler
> providerMock.Expect( p => p.RetrieveFriends() )
>
> Friends target = new Friends(providerMock.Object)
> target.GetFriends()
> providerMock.RaiseEvent( p => p.UserEventHandler, null, new
> UserEventArgs(...) ) <-- this calls the captured handlers.

Is this a suggestion or its available with Moq now? The intellisense
nor the Object Browser show any methods like ExpectAdd or RaiseEvent
in the Moq Namespace.

Thanks

tgmdbm

unread,
Mar 21, 2008, 1:55:30 AM3/21/08
to Moq Discussions
Yes, this is a suggestion for a new feature. What do you think?

Daniel Cazzulino

unread,
Mar 21, 2008, 2:37:27 AM3/21/08
to moq...@googlegroups.com
Indeed, this has ben in the works for a some time now.
It's not there yet, as are facing some minor issues.

> Just like properties are getting their own ExpectGet and ExpectSet
> methods on the Mock<> object, it might be appropriate to add
> ExpectAdd, ExpectRemove, RaiseEvent for event handlers?? RaiseEvent
> would call all the added handlers.

that's our current thinking, yes :)
 
> providerMock.ExpectAdd( p => p.UserEventHandler ) <-- this captures

first problem: this will not compile. C# requires the event reference to always be followed by a += or -= :(. So I do't think there's a way around it being:

providerMock.ExpectAdd( p => p.UserEventHandler += null) <-- this captures

providerMock.RaiseEvent( p => p.UserEventHandler += null, /*sender*/ providerMock.Object, /*args*/ new UserEventArgs(...) ) <-- this calls the captured handlers


Is Mock<T> getting a bit convoluted?


Chaks

unread,
Mar 21, 2008, 5:06:52 AM3/21/08
to Moq Discussions
>
> providerMock.ExpectAdd( p => p.UserEventHandler += null) <-- this captures
>
> providerMock.RaiseEvent( p => p.UserEventHandler += null, /*sender*/
> providerMock.Object, /*args*/ new UserEventArgs(...) ) <-- this calls the
> captured handlers
>

Atleast thats the way we do with RhinoMocks ;)

Chaks

unread,
Mar 21, 2008, 5:10:29 AM3/21/08
to Moq Discussions


On Mar 21, 6:55 pm, tgmdbm <james....@gmail.com> wrote:
> Yes, this is a suggestion for a new feature. What do you think?

This would be an excellent addition :)
Reply all
Reply to author
Forward
0 new messages