Class CastAdapter
- Namespace
- CSF.Screenplay
- Assembly
- CSF.Screenplay.NUnit.dll
An adapter which enables the use of ICast within an NUnit3 test, without needing to parameter-inject the instance
as Lazy<ICast>.
public sealed class CastAdapter : ICast, IHasServiceProvider, IHasPerformanceIdentity
- Inheritance
-
CastAdapter
- Implements
- Inherited Members
Remarks
Due to NUnit architectural limitations, injectable parameters cannot be resolved from DI at the point the test method is built. If we were to attempt this, then the parameter value would not be associated with the correct Screenplay/DI scope (and thus Event Bus). This is due to the two-process model which NUnit uses; one process for building the test methods and another process for running the tests. By using an adapter with Lazy resolution of the real implementation, we ensure that DI resolution is deferred into the test-run process and not the test-building process.
Constructors
CastAdapter(Guid)
Creates a new instance of CastAdapter for the specified performance identity.
public CastAdapter(Guid performanceIdentity)
Parameters
performanceIdentityGuidA performance identity, corresponding to PerformanceIdentity.
Properties
PerformanceIdentity
Gets the unique IPerformance identifier
public Guid PerformanceIdentity { get; }
Property Value
Remarks
This value is used to uniquely identify a performance within a CSF.Screenplay.
ServiceProvider
Gets a service provider/resolver instance associated with this object.
public IServiceProvider ServiceProvider { get; }
Property Value
Methods
GetActor(IPersona)
Gets a single Actor based upon a persona, creating them if they do not already exist in the cast.
public Actor GetActor(IPersona persona)
Parameters
personaIPersonaThe persona from which to get an actor
Returns
- Actor
An actor of the specified name, either an existing instance or a newly-created actor.
Remarks
This method will create the actor within the current cast, using the persona as a factory, if they do not already exist. Alternatively, this method will return the existing actor, if they already exist in the cast, matched using the IPersona's Name.
Actor names are matched using a case-insensitive invariant culture string comparison. Cast implementations should match an existing actor if the specified persona name differs only in case.
Consider using GetActor<TPersona>(ICast) instead of this method; the generic version takes care of resolving the persona instance from dependency injection for you.
- See Also
GetActor(string)
Gets a single Actor by their name, creating them if they do not already exist in the cast.
public Actor GetActor(string name)
Parameters
namestringThe name of the actor to get
Returns
- Actor
An actor of the specified name, either an existing instance or a newly-created actor.
Remarks
This method will create the actor within the current cast, if they do not already exist. Alternatively, this method will return the existing actor, if they already exist in the cast.
Actor names are matched using a case-insensitive invariant culture string comparison. Cast implementations should match an existing actor if the specified name differs only in case.
If you make use of a same-named actor across multiple performances then it is highly recommended to use personas in order to consistently define the actor's attributes and abilities. You would then use the overload of this method which uses that persona to define the actor.
- See Also
GetCastList()
Gets a collection of string names, indicating the collection of actors which this cast object knows about.
public IReadOnlyCollection<string> GetCastList()
Returns
- IReadOnlyCollection<string>
A collection of strings, corresponding to the actors which have been created in the lifetime of the current cast instance.
Remarks
Because a cast instance serves as both a registry and a factory, whenever it is used to get an Actor for the first time, that actor instance is cached within the cast which created it. This method may be used to get a collection of the names of actors which are cached within the current cast instance.
Note that the collection returned by this method is a snapshot in time, correct as-at the time when this method is executed. The collection does not update automatically as new actors are added to the cast. You must execute this method again to get an updated collection.