Mocking a C# timer with Moq

c#, moq, unit-testing

Solution

You should test your code in isolation. Otherwise you don't know whether your code behaves as expected, or there is some side-effect in external dependency. Thus creating mockable wrappers for external resources (configuration files, timers, etc) is the only way you can separate your SUT from external code.

Problem

I'm attempting to create a WPF MVVM View Model that has a dependency injected `System.Timing.Timer`, and wish to test the view model with Moq. I wrote a thin wrapper around the `Timer` class that has a interface `ITimer`, but am unsure of the best way to really test the timer's contribution to the class. Is there a good way to 'force' a mock elapsed event? Does somebody else have a better technique?

Original source

Related problems