Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking a C# timer with Moq

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?

like image 460
Chris Covert Avatar asked Oct 22 '12 19:10

Chris Covert


1 Answers

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.

like image 68
Sergey Berezovskiy Avatar answered Oct 12 '22 17:10

Sergey Berezovskiy