To aid unit-testing we have wrapped up the DateTime class in a delegate so that DateTime.Now can be overridden in a unit-test.
public static class SystemTime
{
#region Static Fields
public static Func<DateTime> Now = () => DateTime.Now;
#endregion
}
Here is an example of its use in an xunit unit-test:
[Fact]
public void it_should_update_the_last_accessed_timestamp_on_an_entry()
{
// Arrange
var service = this.CreateClassUnderTest();
var expectedTimestamp = SystemTime.Now();
SystemTime.Now = () => expectedTimestamp;
// Act
service.UpdateLastAccessedTimestamp(this._testEntry);
// Assert
Assert.Equal(expectedTimestamp, this._testEntry.LastAccessedOn);
}
The test runs fine locally, however it is failing on our build server as the datetimes differ in the Assert statement.
I'm struggling to think of a reason why it would fail given that the DateTime is mocked via the above mentioned delegate wrapper. I've verified there are no issues in the implementation of the UpdateLastAccessedTimestamp method and that the test passes when run locally.
Unfortunately I can't debug it on our build server. Any ideas why it would fail only when run on the build server?
Note that the implementation of UpdateLastAccessedTimestamp is as follows:
public void UpdateLastAccessedTimestamp(Entry entry)
{
entry.LastAccessedOn = SystemTime.Now();
this._unitOfWork.Entries.Update(entry);
this._unitOfWork.Save();
}
Aucun commentaire:
Enregistrer un commentaire