Skip to content

docs(ai): prefer MockDateTimeSources over FakeTimeProvider in .NET tests #1260

Todo

Description

@credfeto

Request

From the owner, in a live session while working on credfeto/recommendations-defi-dashboard#453:

we should be using MockDateTimeSources where possible Pretty sure IDateTimeSource/ICurrentTimeSource no longer exists

q7 yes. However there's an advancing source isn't there too in MockDateTimeSources?

Problem

ai/global/dotnet.instructions.md currently steers agents to FakeTimeProvider:

  • Line ~300 maps "Custom TimeProvider fakes" to FakeTimeProvider from Microsoft.Extensions.TimeProvider.Testing.
  • Lines ~449-452 say to use FakeTimeProvider in tests and to migrate code touching ICurrentTimeSource / IDateTimeSource. Those interfaces no longer exist, so that migration line is stale.

Meanwhile lines ~456-464 already document MockDateTimeSources (Past, Future, AdvancingDateTimeUseWithCaution) from FunFair.Test.Common. MockDateTimeSources.* are TimeProvider instances themselves, so they cover most test needs. Agents following the current text added Microsoft.Extensions.TimeProvider.Testing to every test project in recommendations-defi-dashboard. There, FakeTimeProvider is used in 9 test files, but no test calls Advance, SetUtcNow or AutoAdvanceAmount, so every use could be MockDateTimeSources.Past/Future.

Related (closed, introduced the current rule): #763.

Proposed rule

  • In tests, use MockDateTimeSources.Past / .Future (or .AdvancingDateTimeUseWithCaution when real elapsed time is needed) wherever a TimeProvider is required.
  • Use FakeTimeProvider only when a test must step the clock deterministically (Advance / SetUtcNow), which the advancing source cannot do because it follows real elapsed time.
  • Do not add Microsoft.Extensions.TimeProvider.Testing to a test project that does not use FakeTimeProvider.
  • Remove the stale ICurrentTimeSource / IDateTimeSource migration line, and update the line ~300 mapping to match.
  • Update the credfeto-dotnet-test-patterns / credfeto-dotnet-coding-conventions skill text if it repeats the old rule.

Activity

  1. added
    AI-Work
    Medium
    on Nov 18, 2026
  2. credfeto commented on Nov 18, 2026

    @credfeto
    OwnerAuthor

    Correction: the issue body is wrong about MockDateTimeSources. It says AdvancingDateTimeUseWithCaution "follows real elapsed time" and cannot step the clock deterministically, and so proposes keeping a direct FakeTimeProvider for that case. That is not how it works.

    From funfair-tech/funfair-server-test src/FunFair.Test.Infrastructure/Mocks/MockDateTimeSources.cs:

    public static FakeTimeProvider Past => new(PastDate);
    public static FakeTimeProvider Future => new(FutureDate);
    public static FakeTimeProvider AdvancingDateTimeUseWithCaution =>
        new(PastDate) { AutoAdvanceAmount = TimeSpan.FromDays(1) };
    • Every MockDateTimeSources member returns a FakeTimeProvider, so Advance() and SetUtcNow() are available for on-demand clock steps.
    • AdvancingDateTimeUseWithCaution auto-advances by one day on each read. It has nothing to do with wall-clock time.
    • FunFair.Test.Infrastructure already depends on Microsoft.Extensions.TimeProvider.Testing, so test projects get the package transitively.

    Revised proposed rule:

    • In tests, always obtain a time provider from MockDateTimeSources (Past, Future, or AdvancingDateTimeUseWithCaution). Use Advance() / SetUtcNow() on it when a test needs to move the clock. Never construct FakeTimeProvider directly.
    • Do not add an explicit Microsoft.Extensions.TimeProvider.Testing PackageReference to test projects; it comes through FunFair.Test.Infrastructure.
    • Remove the stale ICurrentTimeSource / IDateTimeSource migration line, and update the line ~300 mapping (custom TimeProvider fakes -> MockDateTimeSources).
  3. self-assigned this
    on Nov 26, 2026
  4. dnyw4l3n13 commented on Nov 26, 2026

    @dnyw4l3n13
    Collaborator

    Implementation Plan

    Files to change

    • ai/global/dotnet.instructions.md - line ~300 mapping: change "Custom TimeProvider fakes" to MockDateTimeSources (Past/Future/AdvancingDateTimeUseWithCaution); lines ~449-452: replace the FakeTimeProvider rule with "always obtain a time provider from MockDateTimeSources, never construct FakeTimeProvider directly, use Advance()/SetUtcNow() on it to move the clock", and remove the stale ICurrentTimeSource/IDateTimeSource migration line (452); lines ~456-464: correct the note that AdvancingDateTimeUseWithCaution follows real time (it auto-advances 1 day per read)
    • CHANGELOG.md - entry under Unreleased (via the changelog tool)

    Approach

    Edit the single instruction file so it matches the owner's corrected rule (comment 6989409348): tests always use MockDateTimeSources.* (each returns a FakeTimeProvider), call Advance()/SetUtcNow() when the clock must move, never construct FakeTimeProvider directly, and do not add an explicit Microsoft.Extensions.TimeProvider.Testing PackageReference because it arrives transitively via FunFair.Test.Infrastructure. The obsolete-interface prohibition on line 450 stays; only the stale migration line is removed. A search of ai/ and .ai-instructions found no other occurrences, so no skill text in this repo needs changes.

    Test strategy

    Docs-only change. Run markdown lint and the pre-commit checks, and grep to confirm no remaining stale FakeTimeProvider/migration wording.

    Assumptions

    a. The credfeto-dotnet-test-patterns / credfeto-dotnet-coding-conventions skills live outside this repo, so they are out of scope.
    b. The owner's correction supersedes the issue body's "use FakeTimeProvider only when stepping" rule.

    Open questions

    None - ready to proceed pending approval

  5. added
    Blocked
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

AI-WorkBlockedMedium

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions