Created
June 14, 2018 09:21
-
-
Save danielplawgo/12ae5d54d2f7a9643ce53c34bb052828 to your computer and use it in GitHub Desktop.
DateTime.Now i podróż w czasie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateService : IDateService | |
{ | |
public DateTime Now | |
{ | |
get | |
{ | |
return DateTime.Now; | |
} | |
} | |
public DateTime UtcNow | |
{ | |
get | |
{ | |
return DateTime.UtcNow; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDateService | |
{ | |
DateTime Now { get; } | |
DateTime UtcNow { get; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public User Update(User user) | |
{ | |
user.UpdatedDate = DateTime.Now; | |
//zapis użytkownika | |
return user; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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