pogorman
pogorman

Reputation: 1711

Moq raise event Parameter count mismatch

I have an interface, with an event I want to fire in a mock:

public interface IGpsLocationSource
{
    event EventHandler<GpsLocation> GpsLocationUpdated;
}

My test look like this:

var gps = new Mock<IGpsLocationSource>();
gps.Raise(x => x.GpsLocationUpdated += (sender, e) => { },  new GpsLocation(0, 0));

I get the following error:

Result StackTrace:
at Moq.Mock1.Raise(Action1 eventExpression, Object[] args) at Test.cs:line 27 Result Message: System.Reflection.TargetParameterCountException : Parameter count mismatch.

What am I doing wrong?

Upvotes: 2

Views: 1187

Answers (2)

Andrew Stephens
Andrew Stephens

Reputation: 10193

If this helps anyone else, I encountered the "parameter count mismatch" issue in a different scenario. I was raising the event using this syntax:

_myMock.Raise(o => o.MyEvent += null, new FoobarEventArgs());

It turned out that the event args class you specify here must inherit from EventArgs, which mine wasnt. Changing the class to the following solved the issue for me:

public class FoobarEventArgs : EventArgs
...

Upvotes: 1

pogorman
pogorman

Reputation: 1711

I forgot the "sender"

gps.Raise(x => x.GpsLocationUpdated += (sender, e) => { }, this, new GpsLocation(0, 0));

Upvotes: 4

Related Questions