# 6. Prefab Tests and Pitfalls
Part of [[Prefab Workflow, Variants and Overrides]]. Previous: [[5. Prefab Scripting Batch Edits and Runtime Instancing]].
## 6.1 Policy tests
`OverridePolicy` is pure C#. Put it in a runtime assembly with No Engine References, or just in your main assembly, and test it in Edit Mode ([[Unity Test Framework]]).
```csharp
using NUnit.Framework;
public sealed class OverridePolicyTests
{
[Test]
public void Glob_Basics()
{
Assert.IsTrue(OverridePolicy.Glob("Transform.m_LocalPosition.*", "Transform.m_LocalPosition.x"));
Assert.IsTrue(OverridePolicy.Glob("*.m_Script", "MonoBehaviour.m_Script"));
Assert.IsTrue(OverridePolicy.Glob("*", ""));
Assert.IsTrue(OverridePolicy.Glob("a*b*c", "a__b__c"));
Assert.IsFalse(OverridePolicy.Glob("a*b*c", "a__b__"));
Assert.IsFalse(OverridePolicy.Glob("Transform.m_LocalPosition.*", "Transform.m_LocalPositionX"));
Assert.IsFalse(OverridePolicy.Glob("GameObject.m_Name", "gameobject.m_name"), "case-sensitive");
}
[Test]
public void Default_AllowsPlacementAndNaming()
{
var p = OverridePolicy.Default();
Assert.AreEqual(OverridePolicy.Verdict.Allowed, p.Check("Transform", "m_LocalPosition.y"));
Assert.AreEqual(OverridePolicy.Verdict.Allowed, p.Check("Transform", "m_LocalRotation.w"));
Assert.AreEqual(OverridePolicy.Verdict.Allowed, p.Check("GameObject", "m_Name"));
}
[Test]
public void Default_DeniesScaleScriptAndMesh()
{
var p = OverridePolicy.Default();
Assert.AreEqual(OverridePolicy.Verdict.Denied, p.Check("Transform", "m_LocalScale.x"));
Assert.AreEqual(OverridePolicy.Verdict.Denied, p.Check("EnemyHealth", "m_Script"));
Assert.AreEqual(OverridePolicy.Verdict.Denied, p.Check("MeshFilter", "m_Mesh"));
}
[Test]
public void Unmatched_IsReview()
{
var p = OverridePolicy.Default();
Assert.AreEqual(OverridePolicy.Verdict.Review, p.Check("EnemyHealth", "maxHealth"));
Assert.AreEqual(OverridePolicy.Verdict.Review, p.Check("MeshRenderer", "m_Materials.Array.data[0]"));
}
[Test]
public void Deny_WinsOverAllow()
{
var p = new OverridePolicy().Allow("Transform.*").Deny("Transform.m_LocalScale.*");
Assert.AreEqual(OverridePolicy.Verdict.Allowed, p.Check("Transform", "m_LocalPosition.x"));
Assert.AreEqual(OverridePolicy.Verdict.Denied, p.Check("Transform", "m_LocalScale.z"));
}
[Test]
public void TeamRule_AllowTunedFieldsOnOneComponent()
{
var p = OverridePolicy.Default().Allow("Light.m_Intensity").Allow("Light.m_Color.*");
Assert.AreEqual(OverridePolicy.Verdict.Allowed, p.Check("Light", "m_Intensity"));
Assert.AreEqual(OverridePolicy.Verdict.Allowed, p.Check("Light", "m_Color.r"));
Assert.AreEqual(OverridePolicy.Verdict.Review, p.Check("Light", "m_Range"));
}
}
```
> [!note] Test status (2026-12-01)
> **6/6 passed** on .NET 10 with a tiny stand-in for NUnit's `Assert`. The file type-checks against Unity 6000.5.8f1's NUnit but **was not run in the Unity Test Runner**.
## 6.2 Prefab invariant tests
The highest-value prefab test loads **every** prefab under a folder and checks the rules your kit depends on. It's an Edit Mode test in an Editor test assembly:
```csharp
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
public sealed class KitPrefabInvariantTests
{
const string KitFolder = "Assets/Kit";
static string[] KitPrefabPaths()
{
var guids = AssetDatabase.FindAssets("t:Prefab", new[] { KitFolder });
var paths = new string[guids.Length];
for (var i = 0; i < guids.Length; i++)
paths[i] = AssetDatabase.GUIDToAssetPath(guids[i]);
return paths;
}
[TestCaseSource(nameof(KitPrefabPaths))]
public void Root_IsAtOrigin_WithUnitScale(string path)
{
var root = PrefabUtility.LoadPrefabContents(path);
try
{
Assert.AreEqual(Vector3.zero, root.transform.localPosition, path);
Assert.AreEqual(Vector3.one, root.transform.localScale, path);
}
finally
{
PrefabUtility.UnloadPrefabContents(root);
}
}
[TestCaseSource(nameof(KitPrefabPaths))]
public void HasNoMissingScripts(string path)
{
var root = PrefabUtility.LoadPrefabContents(path);
try
{
foreach (var t in root.GetComponentsInChildren<Transform>(true))
Assert.AreEqual(0, GameObjectUtility.GetMonoBehavioursWithMissingScriptCount(t.gameObject),
quot;{path}: {t.name}");
}
finally
{
PrefabUtility.UnloadPrefabContents(root);
}
}
}
```
It type-checks against 6000.5.8f1 (NUnit plus `UnityEditor`), but wasn't run. If the folder is empty, `TestCaseSource` produces no test cases, so add one known prefab first. These tests run in CI with `-runTests -testPlatform EditMode` ([[Build Automation and CI]]).
## 6.3 A pre-commit habit
1. Open the **Override Auditor** (part 2), filter to Denied, and fix those.
2. Glance at Review: is each override deliberate?
3. Run the Edit Mode tests.
4. Commit prefabs separately from scenes when you can, so diffs are easy to review.
## 6.4 Pitfalls
| Pitfall | Symptom | Fix |
|---|---|---|
| Editing an instance meaning to edit the asset | One copy differs, the rest don't | Prefab Mode for shared changes; Auditor before commits |
| Apply to the wrong level | Every lamp in the project turns red | Apply to the lowest level that's true (part 2) |
| `Object.Instantiate` in Editor tools | Copies aren't linked; "variants" are regular prefabs | `PrefabUtility.InstantiatePrefab` |
| Deep variant chains | Base changes surprise everyone | At most two levels; numbers in data assets |
| Scale overrides on instances | Colliders and metrics wrong | Variants or new pieces; policy denies scale |
| Unpacking to silence overrides | Fixes to the source never arrive | Revert or apply instead; unpack only for one-offs |
| Removing a component from a base | Variants silently lose it and their overrides | Check variants after base edits |
| Missing prefab asset (deleted or moved outside Unity) | Red "Missing Prefab" instances | Move assets inside the Editor; keep `.meta` files in version control |
| Prefab root not at origin | Instances offset from where designers click | `PrefabStageHooks` warning; invariant test |
| `UnityEditor` in runtime scripts | Build errors | Editor folders, editor asmdefs, `#if UNITY_EDITOR` |
| Forgetting `UnloadPrefabContents` | Leaked preview scenes, memory | `try`/`finally` |
| Scene reference in a prefab field | Works until the scene object is destroyed | `OnValidate` check (`prefab.scene.IsValid()`) |