How to unit test Scheduler using NUnit?

At some point in time you're Scheduler will check the current date and will check it against the scheduled items If you make sure that the 'Check the current date' part is separated in another component you can switch this behavior in your unit tests and let it return a specified date public interface IDateTimeManager { DateTime GetCurrentDateTime(); } class ProductionDateTimeManager : IDateTimeManager { public DateTime GetCurrentDateTime() { throw new NotImplementedException(); } } public class Scheduler { public Scheduler(IDateTimeManager dateTimeManager) { this. DateTimeManager = dateTimeManager; } public Scheduler() { this. DateTimeManager = new ProductionDateTimeManager(); } public void Execute() { DateTime current = DateTimeManager.

GetCurrentDateTime(); } private IDateTimeManager DateTimeManager { get; set; } }.

At some point in time you're Scheduler will check the current date and will check it against the scheduled items. If you make sure that the 'Check the current date' part is separated in another component you can switch this behavior in your unit tests and let it return a specified date. Public interface IDateTimeManager { DateTime GetCurrentDateTime(); } class ProductionDateTimeManager : IDateTimeManager { public DateTime GetCurrentDateTime() { throw new NotImplementedException(); } } public class Scheduler { public Scheduler(IDateTimeManager dateTimeManager) { this.

DateTimeManager = dateTimeManager; } public Scheduler() { this. DateTimeManager = new ProductionDateTimeManager(); } public void Execute() { DateTime current = DateTimeManager. GetCurrentDateTime(); } private IDateTimeManager DateTimeManager { get; set; } }.

Abstract the date and time checking of the scheduler into a separate interface. Have a class which implements this interface for your live code / environment (Using DateTime or whatever is currently used) and one which implements it for your testing enviroment which allows you to accelerate / change time. That way you can test time based functionality quickly and on the fly.

A very simple example, this will vary depending on your scheduler: public interface IDateTimeProvider { DateTime CurrentDateTime { get; } } public class DateTimeProvider : IDateTimeProvider { public DateTime CurrentDateTime { get { return DateTime. Now; } } } public class TestDateTimeProvider : IDateTimeProvider { public DateTime CurrentDateTime { get; set; } } public class Scheduler { private readonly IDateTimeProvider DateTimeProvider; public Scheduler(IDateTimeProvider provider) { DateTimeProvider = provider; } }.

You can use tools like Moles to deal with DateTime. Now angler.wordpress.com/2010/01/21/pex-and-....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions