Skip to content

Give AppSettings an injectable preferences store so its callers are testable #121

Closed

Description

@zhollis21

Problem

AppSettings reads and writes through the static Preferences.Default. That static throws
NotImplementedInReferenceAssemblyException on the plain net10.0 TFM the unit tests build
against, so every code path that reaches AppSettings.Load(), Save(), Clear() or
SyncFromViewer() is untestable.

The concrete casualty today is SettingsPageModel.LoadAsync's authenticated happy path:
PopulateFromUser ends in AppSettings.SyncFromViewer(user) → Save() → Preferences.Default.Set.
A test asserting "successful load → PageState.Content" would actually be asserting on the catch
block, so it was deliberately left unwritten in #62 with a comment saying so. MyAnimePageModel's
list load has the same limit.

Why the obvious fix is bigger than it looks

AppSettings is static for a real reason. Media.DisplayTitle and MediaListEntry.ScoreDisplay
consult it, and those are plain deserialized POCOs that DI never constructs. A full IAppSettings
injection means either threading it through every model, or moving display formatting out of the
models into formatters. Worth doing eventually; not worth blocking test coverage on.

Proposal

Keep the static surface. Make only the storage injectable:

// AppSettings.Storage.cs
internal static IPreferences Storage { get; set; } = Preferences.Default;

MauiProgram assigns the real one at startup; tests assign a dictionary-backed fake.

Two things that make this safer than a static-mutable seam usually is:

  • Preferences.Default returns an implementation off-device and only throws on Get/Set
    (verified), so it is safe as a field initializer — no lazy-init dance, and production behaviour is
    unchanged if nobody assigns it.
  • Cross-test pollution is already handled: AppSettingsCollection (added in Extract AniSprinkles.Core class library to unblock PageModel/Services unit tests #62) serialises every
    test class that writes to the AppSettings statics.

It is a service locator, which is not lovely — but AppSettings is already a process-wide static, so
this does not make the design worse, and the full injection stays available later.

Acceptance

  • AppSettings.Load/Save/Clear/SyncFromViewer are exercisable from tests/AniSprinkles.UnitTests/.
  • SettingsPageModel.LoadAsync's authenticated path has a test asserting PageState.Content and the
    populated profile fields, and the "exception with cached data stays Content" case from Add unit test suite for PageModels, Services, and Converters #52.
  • A round-trip test: set values, Save(), Load() into a fresh state, assert they come back.
  • No production behaviour change — the default storage is still Preferences.Default.

Related

Activity

  1. added
    p3-medium
    Tech Debt
    on Aug 22, 2026
  2. zhollis21 commented on Aug 23, 2026

    @zhollis21
    OwnerAuthor

    Verification notes (checked against origin/main @ 57d4506):

    • Confirmed as written. AppSettings.Storage.cs reaches the static Preferences.Default at 11 call
      sites across Load (:12,18,24,26), Save (:34-37) and Clear (:58-60,62).
    • The Preferences.Default claim checks out empirically. Ran a throwaway net10.0 console
      against Microsoft.Maui.Essentials 10.0.60: the Default getter returns
      Microsoft.Maui.Storage.PreferencesImplementation without throwing, and Get, Set and Remove
      each throw NotImplementedInReferenceAssemblyException. So it is safe as a field initializer —
      no lazy-init dance needed.
    • src/AniSprinkles.Core/Properties/AssemblyInfo.cs:7 already has
      InternalsVisibleTo("AniSprinkles.UnitTests"), so an internal static seam is reachable from the
      test project without widening the public surface.
    • SettingsPageModel already takes an injected IPreferences (SettingsPageModel.cs:195),
      registered at MauiProgram.cs:94 — so the DI half of the wiring exists and the seam only needs
      an assignment at startup.
    • The deliberate gap is recorded in two places that will need updating with the fix:
      tests/AniSprinkles.UnitTests/SettingsPageModelTests.cs:11-17 (the XML comment explaining the
      absent happy-path test) and AGENTS.md:60 ("Known gap ... open in Add unit test suite for PageModels, Services, and Converters #52").
    • AppSettingsCollection (tests/AniSprinkles.UnitTests/AppSettingsCollection.cs) exists and is
      already applied to SettingsPageModelTests, so the serialisation the body relies on is in place.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Tech Debtp3-medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions