logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class ScratchAssetFixture

    One scratch-asset fixture for EditMode tests that create real assets under a throwaway folder and must leave nothing behind: a folder to create assets in, a GUID ledger for the Addressables entries those assets may pick up, and a delete pass that removes both. Replaces the ~60-line "scratch settings asset" fixture ten test suites used to hand-roll on their own — UnitySingletonSettingsLoaderTests, UnityCollectionSettingsLoaderTests, WiredOrDiscoveredSettingsTests, SystemAdoptionRunnerTests, ProjectWiringPinResolutionTests, AdoptedReferenceRemapperTests, AdoptedReferenceEndToEndTests, SerenityAdoptedReferenceValidatorTests, FoundationReferenceIndexTests and SerenityPackageContentRelocatorTests.

    A test owns one instance per [SetUp]/[TearDown] pair, constructed with every scratch folder the test will use (so a crashed previous run's leftovers are still reachable before this test creates anything), and disposes it in [TearDown]:

    private ScratchAssetFixture _fixture;
    
    [SetUp]
    public void SetUp()
    {
        _fixture = new ScratchAssetFixture(PackageScratchFolder, ProjectScratchFolder);
        _fixture.DeleteScratchFolders();
    }
    
    [TearDown]
    public void TearDown() => _fixture.Dispose();

    UnityAddressablesLabeler.RetagAssets is internal to the Global editor assembly and only Tests.Editor carries an InternalsVisibleTo grant for it, not this fixture's own Tests assembly — so retagging a created asset as Addressable stays one call at the test's own call site, right after CreateSettingsAsset<TSettings>(string, string, string, string) or SaveAsset(Object, string, string, string) returns its path.

    Inheritance
    object
    ScratchAssetFixture
    Implements
    IDisposable
    Inherited Members
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    object.GetType()
    Namespace: Serenity.Tests.Shared.Fakes
    Assembly: Tests.dll
    Syntax
    public sealed class ScratchAssetFixture : IDisposable

    Constructors

    ScratchAssetFixture(params string[])

    Declaration
    public ScratchAssetFixture(params string[] scratchFolders)
    Parameters
    Type Name Description
    string[] scratchFolders

    Every folder this test will create assets under. Known upfront so a fresh instance can clear a previous run's leftovers before this test creates anything.

    Fields

    DefaultAssetFileExtension

    Declaration
    public const string DefaultAssetFileExtension = ".asset"
    Field Value
    Type Description
    string

    Properties

    ScratchFolders

    Declaration
    public IReadOnlyCollection<string> ScratchFolders { get; }
    Property Value
    Type Description
    IReadOnlyCollection<string>

    TrackedGuids

    Declaration
    public IReadOnlyList<string> TrackedGuids { get; }
    Property Value
    Type Description
    IReadOnlyList<string>

    Methods

    CreateSettingsAsset<TSettings>(string, string, string, string)

    Creates a real TSettings asset with the given Id/Guid under folderPath, saves and refreshes the AssetDatabase, and tracks its GUID. Does not register the asset as Addressable — call UnityAddressablesLabeler.RetagAssets at the call site afterward when the test needs the fixture registered.

    Declaration
    public string CreateSettingsAsset<TSettings>(string folderPath, string fileName, string id, string guidValue) where TSettings : ScriptableObject, IFoundationSettings
    Parameters
    Type Name Description
    string folderPath
    string fileName
    string id
    string guidValue
    Returns
    Type Description
    string
    Type Parameters
    Name Description
    TSettings

    DeleteScratchFolders()

    Deletes every scratch folder this fixture has touched, wrapped in Serenity.Global.Infrastructure.Editor.Validation.References.SerenitySafeDeleteScope.Suppressed so a legitimate scratch-asset delete does not trip the safe-delete guard. Retries up to Serenity.Tests.Shared.Fakes.ScratchAssetFixture.DeleteFolderMaxAttempts times with an UnityEditor.AssetDatabase.Refresh between attempts: this is the one place that absorbs the temp-folder .meta write-lock flake (observed as Defender still holding a freshly written .meta file for a moment after UnityEditor.AssetDatabase.DeleteAsset(System.String) returns). If a folder still survives every attempt, this throws an IOException naming it instead of leaving it behind for the next test's [SetUp] to also fail to clear — a leaked scratch folder must fail the test that leaked it.

    Declaration
    public void DeleteScratchFolders()

    Dispose()

    Declaration
    public void Dispose()

    EnsureFolder(string)

    Creates the folder (and every missing ancestor) if it does not already exist, and tracks it for DeleteScratchFolders().

    Declaration
    public void EnsureFolder(string folderPath)
    Parameters
    Type Name Description
    string folderPath

    SaveAsset(Object, string, string, string)

    Creates folderPath, writes asset under it as fileName + assetFileExtension, and tracks the resulting GUID. The caller is expected to have already set every field the created asset needs — this only places it on disk.

    Declaration
    public string SaveAsset(Object asset, string folderPath, string fileName, string assetFileExtension = ".asset")
    Parameters
    Type Name Description
    Object asset
    string folderPath
    string fileName
    string assetFileExtension
    Returns
    Type Description
    string

    TrackGuid(string)

    Declaration
    public void TrackGuid(string assetPath)
    Parameters
    Type Name Description
    string assetPath

    TrackGuids(IEnumerable<string>)

    Declaration
    public void TrackGuids(IEnumerable<string> assetPaths)
    Parameters
    Type Name Description
    IEnumerable<string> assetPaths

    Implements

    IDisposable
    In this article
    © 2026 Serenity. All Rights Reserved