-
-
Save vkhorikov/64ff397721cf40cf879c56d8f85ef57e to your computer and use it in GitHub Desktop.
Non-determinism in tests
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 SomeService | |
{ | |
public void FirePeriodicJob() | |
{ | |
/* Runs asynchronously */ | |
} | |
} |
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
[Fact] | |
public void Test() | |
{ | |
var sut = new SomeService(); | |
sut.FirePeriodicJob(); | |
// Give the job time to complete | |
Thread.Sleep(10000); | |
/* Verify the effects of the job */ | |
} |
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
[Fact] | |
public void Test() | |
{ | |
var sut = new SomeService(); | |
sut.FirePeriodicJob(); | |
// Give the job time to complete | |
Task.Delay(10000).Wait(); | |
/* Verify the effects of the job */ | |
} |
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
[Fact] | |
public void Test() | |
{ | |
var sut = new CustomerService(); | |
Customer customer = sut.Create("Name"); | |
Assert.Equal(DateTime.Now, customer.DateCreated); | |
} |
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
[Fact] | |
public void Test() | |
{ | |
DateTime now = DateTime.Now; | |
var sut = new CustomerService(); | |
Customer customer = sut.Create("Name", now); | |
Assert.Equal(now, customer.DateCreated); | |
} |
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 static class SystemDateTime | |
{ | |
private static Func<DateTime> _func; | |
public static DateTime Now | |
{ | |
get { return _func(); } | |
} | |
public static void Init(Func<DateTime> func) | |
{ | |
_func = func; | |
} | |
} |
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
// Initialization code for production | |
SystemDateTime.Init(() => 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
// Initialization code for unit tests | |
SystemDateTime.Init(() => new DateTime(2016, 10, 3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment