Skip to content
All gists
GitHub
Sign up for a GitHub account
Sign in
Create a gist now
Instantly share code, notes, and snippets.
Star
0
Fork
0
vkhorikov
/
1.cs
Last active
Mar 21, 2018
Embed
What would you like to do?
Embed
Embed this gist in your website.
Embed
Share
Copy sharable URL for this gist.
Share
Clone via HTTPS
Clone with Git or checkout with SVN using the repository’s web address.
HTTPS
Learn more about clone URLs
Download ZIP
Code
Revisions
5
Non-determinism in tests
Raw
1.cs
public
class
SomeService
{
public
void
FirePeriodicJob
()
{
/*
Runs asynchronously
*/
}
}
Raw
2.cs
[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
*/
}
Raw
3.cs
[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
*/
}
Raw
4.cs
[Fact]
public
void
Test
()
{
var
sut
=
new
CustomerService
();
Customer
customer
= sut.Create(
"
Name
"
);
Assert.Equal(DateTime.Now, customer.DateCreated);
}
Raw
5.cs
[Fact]
public
void
Test
()
{
DateTime
now
= DateTime.Now;
var
sut
=
new
CustomerService
();
Customer
customer
= sut.Create(
"
Name
"
, now);
Assert.Equal(now, customer.DateCreated);
}
Raw
6.cs
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;
}
}
Raw
7.cs
// Initialization code for production
SystemDateTime.Init
(() => DateTime.UtcNow);
Raw
8.cs
// 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
You can’t perform that action at this time.
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
Press h to open a hovercard with more details.