📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

How would you replace the clock in an ASP.NET Core integration test so retries, cache expiry and your own rules all use the same fake time?

Mid-Level Coding Intermediate

🎯 Key points

  • A controlled clock makes retry, cache and business-rule tests reproducible and fast
  • ConfigureTestServices runs after Program.cs registrations, so AddSingleton<TimeProvider>(fake) overrides TimeProvider.System
  • Resilience pipelines, HybridCache and rate limiting resolve TimeProvider from DI
  • Without it: real backoff waits, sleeps for cache expiry, date-dependent results
  • Code using DateTime.UtcNow or new Timer is not controlled
  • BannedApiAnalyzers: package + BannedSymbols.txt as AdditionalFiles, RS0030 on use

⏱ 30-second answer

Controlling time in an integration test makes results reproducible: a retry delay, a cache expiry and a business cut-off all move together, instead of depending on how fast the CI agent is. Register TimeProvider.System in Program.cs, then in the test call factory.WithWebHostBuilder(b => b.ConfigureTestServices(s => s.AddSingleton<TimeProvider>(fake))) with a FakeTimeProvider. Polly v8 pipelines from Microsoft.Extensions.Resilience, HybridCache and the ASP.NET Core rate limiters resolve TimeProvider from DI, so fake.Advance(...) fires retry delays, expires cache entries and moves your own rules at once. Without it, a retry test waits for real backoff delays (slow), and a cache test either sleeps or never sees an expiry. Code that uses new Timer or DateTime.UtcNow directly won't follow the fake, so ban those APIs with BannedApiAnalyzers: add the Microsoft.CodeAnalysis.BannedApiAnalyzers package and a BannedSymbols.txt (an AdditionalFiles item) listing P:System.DateTime.Now, P:System.DateTime.UtcNow and P:System.DateTimeOffset.UtcNow, which raises RS0030 at build time.

🔍 What the interviewer is checking

Checks they understand which framework components resolve TimeProvider from DI and how to override it in WebApplicationFactory.

↩ Usually asked after

More in Senior Testing Topics

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me