jeudi 29 octobre 2015

DateTime Interface to be used for unit testing

Hi So I have created an interface for datetime like this:

public interface ITimeProvider<T>
{
    T Now { get; }
    string ToShortDateString();
}

And then I have one implamentation of that interface like this:

public class DateTimeProvider : ITimeProvider<DateTime>
{

    private DateTime _date;

    public DateTime Now
    {
        get { return DateTime.Now; }
    }

    public  DateTimeProvider()
    {

        _date = new DateTime();
    }
    public string ToShortDateString()
    {
        return _date.ToShortDateString();
    }
}

Then I am using it in my main unit and want to use property injection but i ran into some problems, here is a snap of what I have:

public class Atm : IAtm
{
   public ITimeProvider _timeProvider { get;set; }
}

This doesn't work as I don't specifiy a type. I could just do

public ITimeProvider<DateTime> _timeProvider { get;set; }

But then I wouldn't be able to use another timeprovider. I have also considered(and is the only solution I could come up with) is to make ATM generic so something like this

 public class Atm<T> : IAtm
 {
      public ITimeProvider<T> _timeProvider { get;set; }
 }

But then I feel like I can't use property injection, is there any other way I can do this so I will be able to test it?

Aucun commentaire:

Enregistrer un commentaire