Testing Infrastructure
Sources
docs/standards.md(testing section) -- NUnit, NSubstitute, coverage expectations, performance testsdocs/development-guide.md(system testing section) --UnitySystemTestBase<T>,[InternalsVisibleTo]
UnitySystemTestBase<TSystem>
Located at Explorer/Assets/DCL/Infrastructure/ECS/TestSuite/UnitySystemTestBase.cs. Provides world lifecycle for ECS system tests.
Key API:
protected TSystem? system-- assign in[SetUp]protected World world-- lazily created with a defaultSceneShortInfo(Vector2Int.zero, "TEST")entityAddTransformToEntity(entity, isDirty, world),AddMaterialToEntity(entity, isDirty, world),AddUITransformToEntity(entity, isDirty)-- convenience helpers delegating toEcsTestsUtils
Key behaviors:
- World is lazily created on first access with a default scene info entity
[TearDown]disposes system and world automatically; overrideOnTearDown()for custom cleanup- Assign
systemin[SetUp]by calling the system constructor directly
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
DCL.EditMode.Tests--Explorer/Assets/DCL/Tests/Editor/DCL.EditMode.Tests.asmdef(platform: Editor only)DCL.PlayMode.Tests--Explorer/Assets/DCL/Tests/PlayMode/DCL.PlayMode.Tests.asmdef(all platforms)
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
- Lifecycle testing: Test creation, update, dirty-flag re-processing, cancellation, and cleanup
- Multi-frame simulation: Call
system.Update(0)repeatedly to simulate multiple frames; setIsDirty = truebetween calls - DeleteEntityIntention: Add
DeleteEntityIntentionto trigger entity destruction cleanup, verify disposal calls via.Received() - IFinalizeWorldSystem: Mock the interface, wire into
ECSWorldFacade, callDispose(), verifyFinalizeComponentscalled - Cinemachine isolation: Tests that assert on
CinemachineBrain.ActiveVirtualCamera(null or a specific camera) must isolate from the staticCinemachineCorequeue — vCams leaked by other fixtures survive across tests. Add aCamerato the brain'sGameObjectwith a culling mask for a dedicated layer, and place only the test's own vCams on that layer. Without this, a disabled vCam still yields a non-nullActiveVirtualCamerafrom another fixture's leftover inTopCameraFromPriorityQueue.
Async Test Patterns
- Use
async Task(notasync void) with[Test] - Simulate
AssetPromiseresolution by addingStreamableLoadingResult<T>to the promise entity, then callsystem.Update(0)to consume - For
UniTaskfailures: useAsyncLoadProcessReport.Create, set exception/cancelled, thenawait WaitUntilFinishedAsync()
Test Organization
Naming
- Class:
{Feature}Should(preferred) or{Feature}Tests - Methods: Describe expected behavior --
CreateAudioSourceFromResolvedPromise,DisposeLoadedScene - Body: Split with
// Arrange,// Act,// Assertcomments
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
- code-standards skill -- test naming conventions, AAA pattern, PR standards
- ecs-system-and-component-design skill -- system test example at the bottom, cleanup lifecycle patterns