Testing infrastructure

From decentraland/unity-explorer · View interactive version

Testing infrastructure is an AI agent skill from the GitHub repository decentraland/unity-explorer. Testing patterns -- UnitySystemTestBase, ECS test utilities, mocking, EditMode vs PlayMode. Use when writing tests for ECS systems, controllers, or async code, or choosing test types.

How to use this skill

Skill file

Testing Infrastructure

Sources


UnitySystemTestBase<TSystem>

Located at Explorer/Assets/DCL/Infrastructure/ECS/TestSuite/UnitySystemTestBase.cs. Provides world lifecycle for ECS system tests.

Key API:

Key behaviors:


Test Utilities

EcsTestsUtils (static helpers in TestSuite)

Located at Explorer/Assets/DCL/Infrastructure/ECS/TestSuite/EcsTestsUtils.cs:

| Method | Purpose |

|--------|---------|

| AddTransformToEntity(world, entity, isDirty) | Creates a GameObject, adds TransformComponent + SDKTransform |

| AddMaterialToEntity(world, entity) | Creates a GameObject, adds MaterialComponent with default URP Lit material |

| SetUpFeaturesRegistry(params string[] flags) | Initializes FeatureFlagsConfiguration and FeaturesRegistry for tests |

| TearDownFeaturesRegistry() | Resets feature flags singletons |

ECSTestUtils (mocking helpers in Tests/Editor)

Located at Explorer/Assets/DCL/Tests/Editor/ECSTestUtils.cs. Use ECSTestUtils.SceneDataSub() whenever a system requires ISceneData -- it maps any content path to a URLAddress.

Note: EcsTestsUtils = static helpers in TestSuite; ECSTestUtils = mocking helpers in Tests/Editor. Different classes despite near-identical names.


EditMode vs PlayMode Tests

| Type | Use when | Requires |

|------|----------|----------|

| EditMode | Pure logic, ECS systems, serialization, utilities -- no scene or coroutines needed | Editor only |

| PlayMode | Integration tests needing MonoBehaviours, scene loading, UniTask.Yield, or full containers | Runtime player loop |

Assembly Structure

Both require UNITY_INCLUDE_TESTS define constraint and reference nunit.framework.dll and NSubstitute.dll.

Feature-local test folders reference the root assembly via .asmref files. Naming convention: {Feature}.Tests.asmref for EditMode (most common), {Feature}.EditMode.Tests.asmref or {Feature}.PlayMode.Tests.asmref when both types exist.

[InternalsVisibleTo] Pattern

System constructors are internal. Expose them to tests via an AssemblyInfo.cs in the production assembly:

[assembly: InternalsVisibleTo("DCL.EditMode.Tests")]
[assembly: InternalsVisibleTo("DCL.PlayMode.Tests")]

Common Mocks

| Interface | Typical setup |

|-----------|---------------|

| ISceneData | ECSTestUtils.SceneDataSub() (maps content paths to URLs) |

| ISceneStateProvider | sceneStateProvider.IsCurrent.Returns(true) |

| IPerformanceBudget | budget.TrySpendBudget().Returns(true) |

| IComponentPoolsRegistry | Mock pool chain: poolsRegistry.GetReferenceTypePool<T>().Returns(pool) |

| IECSToCRDTWriter | Substitute.For<IECSToCRDTWriter>() then verify with .Received() |

| IFinalizeWorldSystem | Substitute.For<IFinalizeWorldSystem>() then verify FinalizeComponents called |

| IRealmData | Substitute.For<IRealmData>() |


System Test Patterns


Async Test Patterns


Test Organization

Naming

Folder Structure

Feature/
  Tests/
    {Feature}Should.cs
    {Feature}.Tests.asmref         -- references DCL.EditMode.Tests
  Tests/EditMode/
    {Feature}.EditMode.Tests.asmref
  Tests/PlayMode/
    {Feature}.PlayMode.Tests.asmref

For full integration tests, use IntegrationTestsSuite.CreateStaticContainer() to spin up the full dependency container.


Detailed Reference

For detailed code examples, see reference.md.


Cross-References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.