简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM