Class MockObjectContext
A mock ObjectContext for unit testing virtual object handlers. State is stored in-memory. Side effects execute inline. Calls and sends are recorded.
public sealed class MockObjectContext : ObjectContext, IObjectContext, ISharedObjectContext, IContext
- Inheritance
-
MockObjectContext
- Implements
- Inherited Members
Constructors
MockObjectContext(string, string?)
Creates a new mock object context with the given key.
public MockObjectContext(string key = "test-key", string? invocationId = null)
Parameters
Properties
Calls
All recorded Call invocations.
public IReadOnlyList<RecordedCall> Calls { get; }
Property Value
Cancellations
All recorded CancelInvocation calls (invocation IDs that were cancelled).
public IReadOnlyList<string> Cancellations { get; }
Property Value
Key
The key of the virtual object instance.
public override string Key { get; }
Property Value
Sends
All recorded Send invocations.
public IReadOnlyList<RecordedSend> Sends { get; }
Property Value
Sleeps
All recorded Sleep invocations with their requested durations.
public IReadOnlyList<RecordedSleep> Sleeps { get; }
Property Value
StateEntryNames
The entry names passed to named state operations, in call order.
public IReadOnlyList<string> StateEntryNames { get; }
Property Value
Methods
Clear(string)
Clears a single state key.
public override void Clear(string key)
Parameters
keystring
Clear(string, string?)
Clears a single state key, labelling the journal entry with
entryName. The name is observability only; the default
implementation drops it and clears through Clear(string).
public override void Clear(string key, string? entryName)
Parameters
ClearAll()
Clears all state keys for this virtual object instance.
public override void ClearAll()
ClearAll(string?)
Clears all state keys for this virtual object instance, labelling the journal entry
with entryName. The name is observability only; the default
implementation drops it and clears through ClearAll().
public override void ClearAll(string? entryName)
Parameters
entryNamestring
GetStateValue<T>(string)
Verifies that a state key was set to the expected value.
public T? GetStateValue<T>(string key)
Parameters
keystring
Returns
- T
Type Parameters
T
Get<T>(StateKey<T>)
Gets the value for the given state key, or default if not set.
public override ValueTask<T?> Get<T>(StateKey<T> key)
Parameters
keyStateKey<T>
Returns
- ValueTask<T>
Type Parameters
T
Get<T>(StateKey<T>, string?)
Gets the value for the given state key, or default if not set, labelling the journal
entry with entryName so it reads as that name in the Restate UI.
The name is observability only: it does not change what is read. The default
implementation drops the name and reads through Get<T>(StateKey<T>),
so contexts that predate entry names keep working.
public override ValueTask<T?> Get<T>(StateKey<T> key, string? entryName)
Parameters
Returns
- ValueTask<T>
Type Parameters
T
HasState(string)
Verifies whether a state key exists.
public bool HasState(string key)
Parameters
keystring
Returns
RegisterClient<TClient>(TClient)
Registers a typed client instance to be returned by typed client methods.
public void RegisterClient<TClient>(TClient client) where TClient : class
Parameters
clientTClient
Type Parameters
TClient
Set<T>(StateKey<T>, T)
Sets the value for the given state key.
public override void Set<T>(StateKey<T> key, T value)
Parameters
keyStateKey<T>valueT
Type Parameters
T
Set<T>(StateKey<T>, T, string?)
Sets the value for the given state key, labelling the journal entry with
entryName so it reads as that name in the Restate UI. The name is
observability only: it does not change what is written. The default implementation
drops the name and writes through Set<T>(StateKey<T>, T), so contexts
that predate entry names keep working.
public override void Set<T>(StateKey<T> key, T value, string? entryName)
Parameters
Type Parameters
T
SetupAwakeable<T>(T)
Enqueues a value to be returned by the next Awakeable<T>(ISerde<T>?) call.
public void SetupAwakeable<T>(T result)
Parameters
resultT
Type Parameters
T
SetupCallFailure(string, string, TerminalException)
Configures a Call to the given service/handler to throw a TerminalException.
public void SetupCallFailure(string service, string handler, TerminalException exception)
Parameters
servicestringhandlerstringexceptionTerminalException
SetupCallFailure(string, string, string, TerminalException)
Configures a Call to the given service/key/handler to throw a TerminalException.
public void SetupCallFailure(string service, string key, string handler, TerminalException exception)
Parameters
servicestringkeystringhandlerstringexceptionTerminalException
SetupCall<T>(string, string, string, T)
Configures the return value for a Call to the given service/key/handler.
public void SetupCall<T>(string service, string key, string handler, T result)
Parameters
Type Parameters
T
SetupCall<T>(string, string, T)
Configures the return value for a Call to the given service/handler.
public void SetupCall<T>(string service, string handler, T result)
Parameters
Type Parameters
T
SetupState<T>(StateKey<T>, T)
Pre-populates a state key for testing.
public void SetupState<T>(StateKey<T> key, T value)
Parameters
keyStateKey<T>valueT
Type Parameters
T
SetupState<T>(string, T)
Pre-populates a state key by name.
public void SetupState<T>(string key, T value)
Parameters
keystringvalueT
Type Parameters
T
StateKeys()
Lists all state keys for this virtual object instance.
public override ValueTask<string[]> StateKeys()
Returns
StateKeys(string?)
Lists all state keys for this virtual object instance, labelling the journal entry with
entryName. The name is observability only; the default
implementation drops it and reads through StateKeys().
public override ValueTask<string[]> StateKeys(string? entryName)
Parameters
entryNamestring