Unit Testing Scheduler in FUTURE C #

I use quartz.NET inside the project scheduler (class library) in my application, because I want other projects to be agnostic for real implementation. In the future, if I want to change the quartz for Castle Scheduler or Windows Scheduler or wathever ... I will have the opportunity to change it.

I need unit test weekly triggers on my Quartz.NET project, I started to study and found out what seemed like a cool MOLES solution at the moment, this extension basically allows me to change DateTime.Now and go to the future!

In this case, a week after the trigger was started, but after waiting a little time, it was sadly found that my triggers were not activated even when the time changed and Thread.Sleeping after a couple of minutes ...

The reason I want to go to the future is because in the application I use different methods / triggers for each type of request EG Weekly, Weekly with repetition, Monthly, Yearly

Has anyone else commented on this test?

Is there something I'm going through?

Is this possible with MOLES?

+4
source share
2 answers

How to implement something like

public interface IClock { DateTime Now { get; } } public class FakeClock : IClock { DateTime Now { get; set; } } public class SystemClock : IClock { DateTime Now { get { return DateTime.Now; } } } 

As the facade develops, you can make your code dependent on IClock by replacing each call on DateTime.Now with IClock.Now.

The IClock dependency can be passed as a constructor parameter or directly for each method that requires it.

Then your production code will use an instance of SystemClock, and your tests can rely on the FakeClock type to manipulate time and make sure that some operation is performed at the expected moment.

This design (Inversion of Control) greatly benefits from working with an Injection Dependency container, such as Castle Windsor, StructureMap, AutoFac, ...

Note. . Further reference, a similar implementation proposal discussed in this post .

+5
source

I have not tested this yet, but when you tried Moles, did you change DateTimeOffset or DateTime? Quartz.Net uses DateTimeOffset.UtcNow:

https://fisheye3.atlassian.com/browse/quartznet/src/Quartz/SystemTime.cs?hb=true

I think you can do it without using moles, although I also have not tested it, just write

SystemTime.UtcNow = () => new DateTimeOffset(DateTime.Now.AddDays(5)).UtcNow

when you want to be five days in the future :)

Please note: SystemTime functionality requires that you build Quartz from the source code, you can get the source in github:

https://github.com/lahma/quartznet

+1
source

Source: https://habr.com/ru/post/1338348/


All Articles