Events
Once a much more prominent feature of the .NET languages, events are not so commonly used anymore. Despite the fall of grace, many libraries, legacy and not, still use this feature.
Moq has some support for events, especially when following established patterns, like using event arguments inheriting from the EventArgs
class. Whilst there is some support for so-called custom events, they will not be part of this guide. Please refer to the Moq Quick Start section related to events for this scenario.
The snippets of this section are based on the following interface
Reacting to method calls
One of the most common scenarios is raising an event upon the invocation of a configured method. This can be done by using the Raises
construct.
Usually, developers would use the Raises construct when the component under test relies on the method being invoked upon a method call. To test the setup to be correct, we can operate on the service as if we were the component under test. We can do this by using the Object
property of the mock.
We then attach an event handler to the event exposed by the interface
Finally, we invoke the Send
method by passing any string.
If the setup is correct, we should see the "Hello world"
string in the output stream.
Reacting to function calls
Like void methods, i.e. methods without return type, Moq can configure functions to raise an event when they are invoked.
The only difference is the requirement for configuring what the method will return before configuring the event Raise
.
Reacting to asynchronous methods
In the case we're dealing with asynchronous methods, few adjustments to the snippet above are required.
The rationale behind the adjustments is that asynchronous methods are normal methods returning a Task
. Moq's strong type system does not make a strong distinction between asynchronous methods and normal methods with a return type.
Raising events manually
Another use case Moq is able to fulfill is the ability to manually raise events. The scenario is the one where the system under test is accepting a service that expose an event, subscribes to this event and reacts when it is raised.
This capability is normally used in the Act part of a properly written unit test.
Last updated