Skip to content
Yauhen Shayunou edited this page Oct 21, 2020 · 1 revision

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 T added to collection and matching specified predicate. If predicate is not provided last artifact of type T is returned. It no artifact found default(T) value is returned.
  • Getting all artifacts of type T matching provided predicate. If no predicate is provided all items of type T are 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.

Setting current actor

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.

Inheritance of current actor

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, Action2 and Action3 are executed against actor1.
  • Action Action4 and expectation Expectation1 are executed against actor2. This example illustrates inheritance of current actor.

Clone this wiki locally