We have repositories which have a “Save” method. They also throw a “Created” event whenever an entity is saved.
We have been trying to use Moq to mock out the repository as such….
var IRepository = new Mock<IRepository>();
Request request = new Request();
IRepository.Setup(a => a.Save(request)).Raises(a => a.Created += null, RequestCreatedEventArgs.Empty);
This doesn’t seem to work and I always get an exception:
System.Reflection.TargetParameterCountException:
Parameter count mismatch.
Any example of mocking events with Moq would be helpful.
A standard event type delegate has two arguments usually: a sender object and a subclass-of-EventArgs object. Moq expects this signature from your event, but only finds one argument and this causes the exception.
Take a look at this code with my comment, it should work: