Unity Editor Mock Mode
Android not availableiOS not availableFlutter not availableReact Native not availableUnity ≥ 1.12.1
Run your Octopus integration inside the Unity Editor, without a device build: the SDK routes its calls through a built-in mock backend, and you can write EditMode tests against the calls it records. This mode is specific to the Unity SDK.
Before you begin
- Add the Octopus Unity SDK to your project: see the Quickstart.
- For EditMode tests, set up the Unity Test Framework (NUnit) in your project.
How it works
In the Editor, OctopusSDK does not call a native SDK. Instead:
- Every call is recorded in a call log,
OctopusSDK.Mock.Calls. - Navigation calls update a simulated screen, shown by an IMGUI overlay.
- Asynchronous calls complete without a network:
ConnectUsercalls your token provider, then completes. - Driver methods of
OctopusSDK.Mockfire the SDK callbacks and events on demand.
The recorded calls, by area:
| Area | Recorded calls |
|---|---|
| Lifecycle | Initialize, SwitchCommunity, Reset, Stop, Close |
| User | ConnectUser, DisconnectUser, RefreshEntitlements, SetHasModifyUserHandler, SetProfileInterceptionEnabled |
| Navigation | Open, OpenPost, OpenCreatePost, OpenGroup, OpenProfile, OpenActivity, OpenMemberActivity, OpenUrlInOctopus, SetUrlInterceptionEnabled |
| Groups | FetchGroups, FollowGroup, UnfollowGroup, SyncFollowGroups |
| Bridges | FetchOrCreateClientObjectRelatedPost, StartObservingClientObjectRelatedPost, StopObservingClientObjectRelatedPost, SetReaction |
| Community data | FetchCommunityData, StartObservingCommunityData, StopObservingCommunityData |
| Analytics and A/B testing | Track, TrackAccessToCommunity, OverrideCommunityAccess |
| Notifications | RegisterNotificationsToken, UpdateNotSeenNotificationsCount |
| Appearance | SetAppName, SetLogo, SetIcons, SetFonts, SetFontWeights, SetLightColorScheme, SetDarkColorScheme, SetColorSchemeType, SetNavBarUsesPrimaryColor, SetForcedOrientation, OverrideDefaultLocale |
All mock code is compiled only in the Editor (#if UNITY_EDITOR): it is absent from device builds.
1. Configure mock mode
Mock mode is on by default: no setup is needed. To change it, set these toggles before OctopusSDK.Initialize(...):
OctopusSDK.Mock.Enabled: bool—falseturns the mock off: the SDK is inert in the Editor.OctopusSDK.Mock.ShowOverlay: bool—falsehides the IMGUI overlay. It can also be changed during Play mode.
// Turn the mock off: the SDK is inert in the Editor
OctopusSDK.Mock.Enabled = false;
// Keep the mock on, but hide the IMGUI overlay
OctopusSDK.Mock.ShowOverlay = false;
OctopusSDK.Initialize("YOUR_API_KEY", ConnectionMode.SSO());
To keep the settings in your project, create an OctopusMockSettings asset at Assets/OctopusSDK/Resources/OctopusMockSettings.asset. At Initialize, its values override the toggles set in code:
| Setting | Effect |
|---|---|
EnabledByDefault | Whether mock mode is on. |
ShowOverlay | Whether the IMGUI overlay is shown. |
InitialNotSeenCount | The count returned by UpdateNotSeenNotificationsCount. |
SeedGroups | The groups returned by FetchGroups and SyncFollowGroups. |
2. Drive callbacks manually
Driver methods simulate what the native SDK would send. They fire even when Mock.Enabled is false.
| Driver | Simulates |
|---|---|
EmitNotSeenCount(int count) | A new count of unseen notifications. |
EmitLoginRequired() | The SDK asking the user to log in. |
EmitGroupsChanged(IList<OctopusGroup> groups) | A change of the groups. |
EmitHasAccessToCommunity(bool hasAccess) | A change of the user's access to the community (A/B test or override). |
EmitNavigateToClientObject(string clientObjectId) | A tap on a button that opens one of your client objects. |
EmitNavigateToProfile(string clientUserId) | A tap on a member's profile. |
EmitModifyUser(ProfileField? field) | The user editing a profile field inside Octopus. |
EmitProfileChanged(OctopusProfile profile) | An update of the user's profile, on the next Unity update. |
EmitGroupAccessDenied(string groupId) | The user being denied access to a group, on the Unity main thread. |
EmitOctopusEvent(OctopusEvent e) | An event of the SDK. |
EmitOctopusEventJson(string json) | An event of the SDK, from its JSON form. |
EmitBridgeShareSignatureRequest(string fingerprint) | The SDK asking your app to sign a Bridge Share image. The JWT your signer returns is recorded in LastBridgeShareSignature. |
OctopusSDK.Mock.EmitNotSeenCount(3);
OctopusSDK.Mock.EmitLoginRequired();
OctopusSDK.Mock.EmitGroupsChanged(myGroups);
OctopusSDK.Mock.EmitHasAccessToCommunity(false);
OctopusSDK.Mock.EmitNavigateToClientObject("article_42");
OctopusSDK.Mock.EmitModifyUser(ProfileField.NICKNAME);
OctopusSDK.Mock.EmitOctopusEvent(new SessionStartedEvent { SessionId = "session_1" });
To test your error handling, set the failure the next call returns. Each one is consumed by the next matching call, then cleared; null means success.
| Setter | Next call that fails |
|---|---|
NextConnectUserError | ConnectUser, callback-based form |
NextGroupFollowUnfollowError | A follow or unfollow of one group |
NextSetReactionError | SetReaction |
NextClientPostError | FetchOrCreateClientObjectRelatedPost |
RefreshEntitlementsError | RefreshEntitlements, until you set it back to null |
To seed data, use SetGroups(groups) for the groups, SetClientObjectRelatedPost(objectId, post) for the post of a client object, and SetCommunityData(memberId, data) for the community data of a member.
3. Write EditMode tests
OctopusSDK.Mock keeps a log of every call. Call OctopusSDK.Mock.Reset() in [SetUp] to clear it between tests, then check the results:
- Typed accessors:
LastOpenedPost,LastPrefilledPost,LastTrackedEvent,CurrentScreen,LastBridgeShareSignature,LastBridgeShareSignatureFailed. LastCall("MethodName")— the most recent call with that name, ornull.Calls— an orderedIReadOnlyList<Call>of every call since the lastReset(). EachCallhas aMethodstring and anArgsarray.
using NUnit.Framework;
public class MyOctopusTests
{
[SetUp]
public void SetUp()
{
// Reset() does not restore Enabled: set it again in case a test turned it off
OctopusSDK.Mock.Enabled = true;
OctopusSDK.Mock.Reset();
}
[Test]
public void OpenPost_RecordsPostId()
{
OctopusSDK.OpenPost("42");
Assert.AreEqual("42", OctopusSDK.Mock.LastOpenedPost);
}
[Test]
public void OpenCreatePost_RecordsPrefill()
{
OctopusSDK.OpenCreatePost(new OctopusPrefilledPost
{
Text = "Hello",
TopicId = "group_1"
});
Assert.AreEqual("Hello", OctopusSDK.Mock.LastPrefilledPost.Text);
Assert.AreEqual("group_1", OctopusSDK.Mock.LastPrefilledPost.TopicId);
}
}
Behavior and limits
Reset()clears the call log, the current screen, the typed accessors, the failure setters and the seeded data. It does not changeEnabledorShowOverlay.- Events emitted with
EmitOctopusEventare raised synchronously, on the thread that calls the driver. On a device, they are raised on a background thread: see Analytics. - The mock does not reproduce the behavior of the backend: moderation, permissions and the content of the community are not simulated.
Next steps
- Analytics — listen to the events you emit with
EmitOctopusEvent. - Events reference — the events you can simulate.
- Bridges — the client object flows you can seed with
SetClientObjectRelatedPost.