17 November 2023

17 November 2023

Avoiding flaky tests with TimeProvider and ITimer 🔗

Fixing the age-old problem of testing code that uses time. C# 8 introduces an abstract class TimeProvider that can be used to make code testable. You might think this is similar to what you could have done yourself by writing an interface around time methods, but this has the added bonus of being able to manipulate time. And more importantly, many .Net methods such as Task.Delay accept TimeProvider as a parameter. And those overloads are back-ported to netstandard2.0.

To actually do the time manipulation in testing, you use the Microsoft.Extensions.TimeProvider.Testing package. With that you can do things like this:

var fakeTime = new FakeTimeProvider(startDateTime: DateTimeOffset.UtcNow);
// inject this into all the things
// do something
// assert something that is supposed to happen 2 seconds later

ConfigureAwait in .NET 8 🔗

Another age-old problem is ConfigureAwait, and async/await in general. In .Net 8 they've added more options. I'm not sure that that makes it simpler, but at least there's more options! Previoulsy you would generally use


Now you can use


Or one of the other three values of ConfigureAwaitOptions.

Spammers abuse Google Forms' quiz to deliver scams 🔗

Clever spammers! They found a way to use the quiz feature of Google Forms to send spam emails to people. Because the email originated from Google, it has a better chance of getting through spam filters.