I raised the question of support for mocking delegates just a few days ago:
If that were possible, then you could write code as you suggested.
var clickHandler = _mock.Create<EventHandler<ClickEventArgs>>();
button.Click += clickHandler.Object;
clickHandler.Setup(h => h(button, new ClickEventArgs(MouseButton.Left)));
The signature of
the event is defined by the delegate, and it seems to me a bit silly to have to define an interface with a single member just to allow testing of what you are talking about.
Daniel's response to my question was:
> Don't think we'll support this anytime soon (unless we get a patch, as usual :))
Following this response I have some questions. First, to those who control the philosophy/direction of Moq (and consequently, commits of patches):
- Is this something you just don't have bandwidth for, or does it cut against the grain of where you see Moq heading? Asked another way, would a patch that supports this be accepted?
Questions for the whole group:
- How many people would like to see support for mocking delegates added?
- Has anyone tried this and hit any barriers?
- Can anyone improve upon the code sample given above?
Drew.