Swap dependencies for tests and platforms
Swap a dependency from code
To replace a dependency in a test, or at runtime for a cheat or a mod, perform the following steps:
- Create a registry, and add it before every other provider with
Injector.AddFirst. - Add the replacement to the registry.
- When you no longer need the replacement, remove the registry with
Injector.Remove.
RegistryProvider stubs = new();
Injector.AddFirst(stubs); // 1. Tried before the provider list
stubs.Add<IAnalytics>(new FakeAnalytics()); // 2. The replacement
MatchReport report = new(); // a plain class: its [Inject] IAnalytics member receives FakeAnalytics
Injector.Remove(stubs); // 3. The other providers answer again
Step 3 is optional in a test run in the Editor and in Play mode: Selvage removes a provider added during a Test Runner
test when the test ends, and a provider added in Play mode when you exit Play mode. A test run on a player keeps what
it added, so remove it in the test's TearDown.
A member that was injected before the swap keeps its dependency, unless it's
Reactive. To give it the replacement, call
Manual.Reinject.
Swap a class with an assembly
A class marked [Provide] in an assembly that only exists in some cases replaces the classes of the
other assemblies in those cases. No code chooses between the classes: the assembly each one is in decides.
// In your game assembly
[Provide]
public class SteamStore : IStore { ... }
// In an Editor assembly: IStore members receive MockStore in the Editor, and SteamStore in a build.
[Provide]
public class MockStore : IStore { ... }
For a requested type, the classes of the highest-ranked assembly replace those of every lower rank:
| Rank | Assembly | Its classes apply |
|---|---|---|
| 1 | Test assembly | While a Test Runner run that includes the assembly is in progress. |
| 2 | Editor assembly: an Editor folder, or an assembly definition limited to the Editor platform | In the Editor, in Edit mode and in Play mode. |
| 3 | Assembly definition with platforms or define constraints | Whenever it's compiled. |
| 4 | Project assembly, under Assets | Always. |
| 5 | Package assembly, under Packages | Always. |
The rank applies to the classes the Attribute Provider gives. The order of the provider list still comes first, so an object in the Registry Provider wins over any class. The Dependencies window names the class that gives the dependency and the class it replaces. The Validation tab reads the assemblies of the Editor and of the current build target, and updates when you switch the target.
Swap a class from a provider
To choose a class from code, write a provider that picks it, then add the provider to the provider list:
[Serializable]
public sealed class InputProvider : Provider
{
protected override Verdict Evaluate(Request request)
=> (request.Type == typeof(IInput)) ? Verdict.Always() : Verdict.Never;
protected override object ShareKey(Request request, object context, object channel)
=> typeof(IInput);
protected override Supply Provide(Request request, object context, object channel)
=> Supply.Owned((Application.isMobilePlatform) ? new TouchInput() : new DesktopInput());
}
For more information, refer to Write a custom provider.