-
Notifications
You must be signed in to change notification settings - Fork 0
Actors
Each scenario uses at least on actor (either implicitly or explicitly). It no explicit actor is define implicit actor is used by scenario.
Actor is container of artifacts which are produced during scenario execution and be used to define actions and expectations. Contract of actor looks as follows:
public interface IActor
{
IArtifactCollection Artifacts { get; }
}Artifact collection contract is defined as follows:
public interface IArtifactCollection
{
void Add(object artifact);
T Get<T>(Func<T, bool> predicate = null);
T[] GetAll<T>(Func<T, bool> predicate = null);
void Clear();
}So collection can do the following:
- Adding artifact to collection of artifacts associated with current actor.
- Getting last artifact of type
Tadded to collection and matching specified predicate. If predicate is not provided last artifact of typeTis returned. It no artifact founddefault(T)value is returned. - Getting all artifacts of type
Tmatching provided predicate. If no predicate is provided all items of typeTare returned. Items are sorted in reverse order of how they were added. E.g. latest added items is going to be first item of returned result. - Clearing artifact collection. All artifacts in artifact collection are removed.
Each of the following methods has overload which allows to change current actor:
Given()When()Then()
Here is example which define scenario for two actors:
Scenario.New()
.Given(actor1)
.Performs<HttpRequest>(x => x
.Method("POST")
.Url("/v1/documents"))
.When(actor2)
.Performs<HttpRequest>(x => x
.Method("GET")
.Url("/v1/documents"))
.Then(actor1)
.Expects<HttpResponse>(x => x
.Status(HttpStatusCode.OK))
.Then(actor2)
.Expects<HttpResponse>(x => x
.Status(HttpStatusCode.OK))
.Run();In this scenario actor1 creates document and actor2 retrieves list of all documents. Expectations are executed against actor specified in Then() method.
IMPORTANT: Only artifacts of current actor can be used to construct actions and expectations.
Let's review the following example:
Scenario.New()
.Given(actor1)
.Performs(..)
.Performs(..)
.When()
.Performs(..)
.When(actor2)
.Performs(..)
.Then()
.Expects(..)
.Run();
There is no need to specify current actor for each invocation of Given(), When(), Then() methods. If actor is not specified previously defined current actor is used (or default one). In suggested examples the following happens:
- Actions
Action1,Action2andAction3are executed againstactor1. - Action
Action4and expectationExpectation1are executed againstactor2. This example illustrates inheritance of current actor.