Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created June 14, 2018 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielplawgo/12ae5d54d2f7a9643ce53c34bb052828 to your computer and use it in GitHub Desktop.
Save danielplawgo/12ae5d54d2f7a9643ce53c34bb052828 to your computer and use it in GitHub Desktop.
DateTime.Now i podróż w czasie
public class DateService : IDateService
{
public DateTime Now
{
get
{
return DateTime.Now;
}
}
public DateTime UtcNow
{
get
{
return DateTime.UtcNow;
}
}
}
public class DateService : IDateService
{
private TimeSpan _offset = new TimeSpan();
public DateTime Now
{
get
{
return DateTime.Now + _offset;
}
}
public DateTime UtcNow
{
get
{
return DateTime.UtcNow + _offset;
}
}
public void SetNow(DateTime date)
{
_offset = date - DateTime.Now;
}
public void Reset()
{
_offset = new TimeSpan();
}
}
public interface IDateService
{
DateTime Now { get; }
DateTime UtcNow { get; }
}
public User Update(User user)
{
user.UpdatedDate = DateTime.Now;
//zapis użytkownika
return user;
}
public User Create(string name)
{
if (DateTime.Now < new DateTime(2017, 9, 1))
{
throw new Exception("Rejestracja użytkowników jest zamknięta do 1 września 2017.");
}
User user = new User();
user.Name = name;
//dodanie użytkownika
return user;
}
public class UserLogic
{
private IDateService _dateService;
public UserLogic(IDateService dateService)
{
_dateService = dateService;
}
public User Update(User user)
{
user.UpdatedDate = _dateService.Now;
//zapis użytkownika
return user;
}
}
[TestFixture]
public class UserLogicTests
{
private Mock<IDateService> _dateServiceMock;
private UserLogic Create()
{
_dateServiceMock = new Mock<IDateService>();
return new UserLogic(_dateServiceMock.Object);
}
[Test]
public void Update_SetUpdatedDate()
{
var userLogic = Create();
_dateServiceMock.Setup(u => u.Now)
.Returns(new DateTime(2017, 1, 1));
var user = userLogic.Update(new User());
Assert.AreEqual(new DateTime(2017, 1, 1), user.UpdatedDate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment