'Mocking windows event log for unit tests
Part of my application deals with subscribing to the Windows event log and listening for events to be logged via EventLogWatcher:
Allows you to subscribe to incoming events. Each time a desired event is published to an event log, the EventRecordWritten event is raised, and the method that handles this event will be executed.
When a specific event(based on a query I pass into EventLogWatcher's constructor) is published, my custom DoSomething() function gets called via the EventRecordWritten event that gets raised and does some verification logic. The unit test I'm trying to create would ensure that DoSomething() does verification logic correctly.
I realize I'm supposed to separate external dependencies from my unit test(s) so I'm trying to mock this process. Is it possible to mock the windows event log in some way so that I can ultimately get DoSomething() to be called?
Solution 1:[1]
The unit test I'm trying to create would ensure that
DoSomething()does verification logic correctly.
This would tell me you shouldn't worry about the event log at all for this unit test. You should call DoSomething() directly from your unit test with sample parameters and see whether it applies its logic correctly.
Presumably there's another class responsible for configuring the event log watcher to invoke DoSomething(). If you want to make sure that's being done correctly, you should inject the interface with DoSomething() into that class so you can mock that service and create a separate test that causes an event log to be emitted and verifies that DoSomething() is invoked on a mock it injects into that class.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | StriplingWarrior |
