Table of Contents

Class Operation<TRequest, TResponse, TState>

Namespace
Microsoft.Accordant
Assembly
Accordant.Operations.dll

The unified base class for operations.

An Operation defines:

public abstract class Operation<TRequest, TResponse, TState> : IOperation where TState : class, IState

Type Parameters

TRequest

The type of request this operation accepts.

TResponse

The type of response this operation returns.

TState

The 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

name string

The 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

IReadOnlyList<RequestDerivation>

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

string

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

PollingSetup

RequestType

The Type of the request sent to this operation.

public Type RequestType { get; }

Property Value

Type

ResponseType

The Type of the response received from this operation.

public Type ResponseType { get; }

Property Value

Type

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

request TRequest

The request to process.

state TState

The 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

context TestingContext

The testing context.

request TRequest

The 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

context TestingContext

The testing context.

request TRequest

The 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

request TRequest
state IState
observedResponse object

Returns

string

ExplainInvalidResponse(TRequest, StateProfile, object)

public string ExplainInvalidResponse(TRequest request, StateProfile stateProfile, object observedResponse)

Parameters

request TRequest
stateProfile StateProfile
observedResponse object

Returns

string

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

request TRequest
state IState

Returns

IList<(TResponse, StateProfile)>

Verify(TRequest, IState, object)

public (bool, StateProfile) Verify(TRequest request, IState state, object observedResponse)

Parameters

request TRequest
state IState
observedResponse object

Returns

(bool, StateProfile)

Verify(TRequest, StateProfile, object)

public (bool, StateProfile) Verify(TRequest request, StateProfile stateProfile, object observedResponse)

Parameters

request TRequest
stateProfile StateProfile
observedResponse object

Returns

(bool, StateProfile)

With(string)

Creates an OperationInput for a parameterless operation (where TRequest is Unit).

public OperationInput With(string label)

Parameters

label string

An 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

request TRequest

The request to bind.

label string

An optional label for the input. Defaults to the operation name.

Returns

OperationInput

An OperationInput instance.