Class Operation<TRequest, TResponse, TState>
The unified base class for operations.
An Operation defines:
- The spec/behavior via Apply(TRequest, TState) (required)
- Execution logic via ExecuteAsync(TestingContext, TRequest) (optional)
- Request derivations via DerivedFrom (optional)
- Polling setup via Polling (optional)
public abstract class Operation<TRequest, TResponse, TState> : IOperation where TState : class, IState
Type Parameters
TRequestThe type of request this operation accepts.
TResponseThe type of response this operation returns.
TStateThe type of state this operation operates on.
- Inheritance
-
Operation<TRequest, TResponse, TState>
- Implements
- Inherited Members
- Extension Methods
Constructors
Operation(string)
Constructs an instance of this operation with the given name.
protected Operation(string name)
Parameters
namestringThe name of the operation.
Properties
DerivedFrom
The request derivations for this operation. Override this property to declare how this operation's requests can be derived from other operations' request/response pairs. Returns an empty list by default (no allocation).
public virtual IReadOnlyList<RequestDerivation> DerivedFrom { get; }
Property Value
ExecuteFunc
An optional function to execute the operation. This can be set as an alternative to overriding ExecuteAsync(TestingContext, TRequest).
public Func<TestingContext, TRequest, Task<TResponse>> ExecuteFunc { get; set; }
Property Value
- Func<TestingContext, TRequest, Task<TResponse>>
Expect
Provides type-inferred expect methods for use within the Apply(TRequest, TState) method.
Using this property allows writing Expect.That(r => ...) and .ThenState(s => ...)
without specifying the generic type parameters, since they are inferred from the operation's
TResponse and TState types.
protected ExpectContext<TResponse, TState> Expect { get; }
Property Value
- ExpectContext<TResponse, TState>
Examples
public override ExpectedOutcomes Apply(int value, StackState state) { // No need for Expect.That<int> or .ThenState<StackState> return Expect.That(r => r == value, "should equal value") .ThenState(nextState => nextState.Items.Add(value)); }
Name
The name of the operation. This is used to register and look up the operation in a Spec<TState>.
public string Name { get; }
Property Value
Polling
The polling setup for this operation. Override this property if the operation triggers background async work via a TerminatingStepFunction that needs polling. The polling request is created using a derivation defined on the polling operation. Returns null by default (no polling).
public virtual PollingSetup Polling { get; }
Property Value
RequestType
The Type of the request sent to this operation.
public Type RequestType { get; }
Property Value
ResponseType
The Type of the response received from this operation.
public Type ResponseType { get; }
Property Value
Spec
Reference to the Spec<TState> this operation is registered with. This is set automatically when the operation is added to a Spec.
public Spec<TState> Spec { get; }
Property Value
- Spec<TState>
Methods
Apply(TRequest, TState)
Defines the behavior of this operation by returning the expected outcomes given a request and current state.
This is the core specification method that must be implemented.
public abstract ExpectedOutcomes Apply(TRequest request, TState state)
Parameters
requestTRequestThe request to process.
stateTStateThe current state.
Returns
- ExpectedOutcomes
The expected outcomes including response descriptor and next state(s).
Execute(TestingContext, TRequest)
Executes this operation synchronously against the system-under-test.
Override this method for simple synchronous operations. For async operations, override ExecuteAsync(TestingContext, TRequest) instead.
public virtual TResponse Execute(TestingContext context, TRequest request)
Parameters
contextTestingContextThe testing context.
requestTRequestThe request to execute.
Returns
- TResponse
The response from the system.
Exceptions
- NotImplementedException
Thrown if neither Execute nor ExecuteAsync is overridden.
ExecuteAsync(TestingContext, TRequest)
Executes this operation asynchronously against the system-under-test.
Override this method for async operations, or override Execute(TestingContext, TRequest) for simple synchronous operations. You can also set ExecuteFunc as an alternative.
If only Execute(TestingContext, TRequest) is overridden, this method automatically calls it and wraps the result in a completed task.
public virtual Task<TResponse> ExecuteAsync(TestingContext context, TRequest request)
Parameters
contextTestingContextThe testing context.
requestTRequestThe request to execute.
Returns
- Task<TResponse>
The response from the system.
ExplainInvalidResponse(TRequest, IState, object)
public string ExplainInvalidResponse(TRequest request, IState state, object observedResponse)
Parameters
Returns
ExplainInvalidResponse(TRequest, StateProfile, object)
public string ExplainInvalidResponse(TRequest request, StateProfile stateProfile, object observedResponse)
Parameters
requestTRequeststateProfileStateProfileobservedResponseobject
Returns
Invoke(TRequest, IState)
Returns all possible next states and optional step functions given a request and state.
public IList<(TResponse, StateProfile)> Invoke(TRequest request, IState state)
Parameters
requestTRequeststateIState
Returns
- IList<(TResponse, StateProfile)>
Verify(TRequest, IState, object)
public (bool, StateProfile) Verify(TRequest request, IState state, object observedResponse)
Parameters
Returns
- (bool, StateProfile)
Verify(TRequest, StateProfile, object)
public (bool, StateProfile) Verify(TRequest request, StateProfile stateProfile, object observedResponse)
Parameters
requestTRequeststateProfileStateProfileobservedResponseobject
Returns
- (bool, StateProfile)
With(string)
Creates an OperationInput for a parameterless operation (where TRequest is Unit).
public OperationInput With(string label)
Parameters
labelstringAn optional label for the input. Defaults to the operation name.
Returns
- OperationInput
An OperationInput instance.
Examples
// Instead of: Spec.Pop.With(Unit.Value, "Pop") // Use: Spec.Pop.With("Pop")
With(TRequest, string)
Creates an OperationInput with the given request bound to this operation.
public OperationInput With(TRequest request, string label = null)
Parameters
requestTRequestThe request to bind.
labelstringAn optional label for the input. Defaults to the operation name.
Returns
- OperationInput
An OperationInput instance.