Roles

Spy

static

Creates spies: the real member runs, and what the code under test does with it is recorded. A spy belongs to the NUnit test or fixture that creates it, and starts fresh in every test. Explained in Check what the code under test did and Spy on caught exceptions.

Every method throws InvalidOperationException outside an NUnit test, or when its selection was not woven (Catch, which takes no selection, only outside an NUnit test).

Static methods
SpiedMethod<TResult> Method<TResult>(Func<TResult> selection)Spies on a method with a result, an operator or a conversion: () => Object.Instantiate(bulletPrefab).
SpiedMethod Method(Action selection)Spies on a void method: () => Object.Destroy(enemy.gameObject).
SpiedMethodTable<TInstance, TResult> Method<TInstance, TResult>(Func<TInstance, TResult> selection)Spies on a method with a result on any instance, one spy per instance: (Weapon w) => w.Damage().
SpiedMethodTable<TInstance> Method<TInstance>(Action<TInstance> selection)Spies on a void method on any instance, one spy per instance: (Weapon w) => w.Fire().
SpiedConstructor<TResult> Constructor<TResult>(Func<TResult> selection)Spies on a constructor: () => new SaveSystem().
SpiedField<TValue> Field<TValue>(Func<TValue> selection)Spies on a field's reads and writes: () => player.health.
SpiedFieldTable<TInstance, TValue> Field<TInstance, TValue>(Func<TInstance, TValue> selection)Spies on a field's reads and writes on any instance, one spy per instance: (Enemy e) => e.health.
SpiedProperty<TValue> Property<TValue>(Func<TValue> selection)Spies on a property's reads and writes: () => Time.timeScale.
SpiedPropertyTable<TInstance, TValue> Property<TInstance, TValue>(Func<TInstance, TValue> selection)Spies on a property's reads and writes on any instance, one spy per instance: (Rigidbody b) => b.linearVelocity.
SpiedIndexer<TValue> Indexer<TValue>(Func<TValue> selection)Spies on the reads and writes of an element written with []: an array element or an indexer, () => level.tiles[Arg.Any<int>()].
SpiedIndexerTable<TInstance, TValue> Indexer<TInstance, TValue>(Func<TInstance, TValue> selection)Spies on the reads and writes of an element written with [] on any array or indexed object, one spy per instance: (Tile[] t) => t[Arg.Any<int>()].
SpiedEvent Event(Action selection)Spies on an event, named by a subscription of Arg.Ignored: () => player.Died += Arg.Ignored.
SpiedEventTable<TInstance> Event<TInstance>(Action<TInstance> selection)Spies on an event on any instance, one spy per instance: (Enemy e) => e.Killed += Arg.Ignored.
SpiedCatch<TException> Catch<TException>() where TException : ExceptionSpies on the catch blocks the code under test enters with an exception of this type, subtypes included.

Stub

static

Creates stubs: the member is replaced with the answers the test sets with Returns and Do, and each access the selection matches is recorded as a spy records it. With nothing declared, a stub does the C# default. A stub belongs to the NUnit test or fixture that creates it, starts fresh in every test, and the last stub, fake or Real declared on an access decides it. Explained in Stub members.

Every method throws InvalidOperationException outside an NUnit test, or when its selection was not woven.

Static methods
StubbedMethod<TResult> Method<TResult>(Func<TResult> selection)Stubs a method with a result, an operator or a conversion: () => Random.Range(0, 10).
StubbedMethod Method(Action selection)Stubs a void method: () => Object.Destroy(Arg.Any<Object>()).
StubbedMethodTable<TInstance, TResult> Method<TInstance, TResult>(Func<TInstance, TResult> selection)Stubs a method with a result on any instance, one stub per instance: (Weapon w) => w.Damage().
StubbedMethodTable<TInstance> Method<TInstance>(Action<TInstance> selection)Stubs a void method on any instance, one stub per instance: (Rigidbody b) => b.AddForce(Arg.Any<Vector3>()).
StubbedConstructor<TResult> Constructor<TResult>(Func<TResult> selection)Stubs a constructor: () => new SaveSystem(). Code never expects null from new, so give it a Returns.
StubbedField<TValue> Field<TValue>(Func<TValue> selection)Stubs a field's reads and writes: () => sensor.distance.
StubbedFieldTable<TInstance, TValue> Field<TInstance, TValue>(Func<TInstance, TValue> selection)Stubs a field's reads and writes on any instance, one stub per instance: (Enemy e) => e.health.
StubbedProperty<TValue> Property<TValue>(Func<TValue> selection)Stubs a property's reads and writes: () => Time.deltaTime.
StubbedPropertyTable<TInstance, TValue> Property<TInstance, TValue>(Func<TInstance, TValue> selection)Stubs a property's reads and writes on any instance, one stub per instance: (Collision c) => c.gameObject.
StubbedIndexer<TValue> Indexer<TValue>(Func<TValue> selection)Stubs the reads and writes of an element written with []: an array element or an indexer, () => level.grid[Arg.Any<int>(), Arg.Any<int>()].
StubbedIndexerTable<TInstance, TValue> Indexer<TInstance, TValue>(Func<TInstance, TValue> selection)Stubs the reads and writes of an element written with [] on any array or indexed object, one stub per instance: (Tile[] t) => t[Arg.Any<int>()].

Fake

static

Creates fakes: the member is replaced with a working in-memory stand-in, a stored value for a field or property, stored handlers for an event, and each access the selection matches is recorded as a spy records it. A fake belongs to the NUnit test or fixture that creates it, starts fresh in every test, and the last stub, fake or Real declared on an access decides it. Explained in Fake fields and properties and Fake events.

Every method throws InvalidOperationException outside an NUnit test, or when its selection was not woven.

Static methods
FakedField<TValue> Field<TValue>(Func<TValue> selection, TValue initial = default)Fakes a field with a stored value, one per instance the selection reaches: () => player.health. initial is the initial value each stored value starts at, in every test.
FakedField<TValue> Field<TValue>(Func<TValue> selection, Func<TValue> initial)Fakes a field with a stored value, one per instance the selection reaches, each created by a factory: Fake.Field(() => player.inventory, initial: () => new List<Item>()). Throws ArgumentNullException when the factory is null.
FakedFieldTable<TInstance, TValue> Field<TInstance, TValue>(Func<TInstance, TValue> selection, TValue initial = default)Fakes a field on any instance, one stored value per instance: (Gun g) => g.ammo.
FakedFieldTable<TInstance, TValue> Field<TInstance, TValue>(Func<TInstance, TValue> selection, Func<TValue> initial)Fakes a field on any instance, one stored value per instance, each created by a factory. Throws ArgumentNullException when the factory is null.
FakedFieldTable<TInstance, TValue> Field<TInstance, TValue>(Func<TInstance, TValue> selection, Func<TInstance, TValue> initial)Fakes a field on any instance, one stored value per instance, each created from its instance: Fake.Field((Enemy e) => e.inventory, initial: (Enemy e) => new List<Item>()). Throws ArgumentNullException when the factory is null.
FakedProperty<TValue> Property<TValue>(Func<TValue> selection, TValue initial = default)Fakes a property with a stored value, one per instance the selection reaches: () => Time.timeScale. initial is the initial value each stored value starts at, in every test.
FakedProperty<TValue> Property<TValue>(Func<TValue> selection, Func<TValue> initial)Fakes a property with a stored value, one per instance the selection reaches, each created by a factory. Throws ArgumentNullException when the factory is null.
FakedPropertyTable<TInstance, TValue> Property<TInstance, TValue>(Func<TInstance, TValue> selection, TValue initial = default)Fakes a property on any instance, one stored value per instance: (Rigidbody b) => b.linearVelocity.
FakedPropertyTable<TInstance, TValue> Property<TInstance, TValue>(Func<TInstance, TValue> selection, Func<TValue> initial)Fakes a property on any instance, one stored value per instance, each created by a factory. Throws ArgumentNullException when the factory is null.
FakedPropertyTable<TInstance, TValue> Property<TInstance, TValue>(Func<TInstance, TValue> selection, Func<TInstance, TValue> initial)Fakes a property on any instance, one stored value per instance, each created from its instance. Throws ArgumentNullException when the factory is null.
FakedEvent Event(Action selection)Fakes an event, named by a subscription of Arg.Ignored: () => SceneManager.sceneLoaded += Arg.Ignored. Subscriptions from the code under test land in the fake, and the test raises it with Raise.
FakedEventTable<TInstance> Event<TInstance>(Action<TInstance> selection)Fakes an event on any instance, one fake per instance: (Enemy e) => e.Killed += Arg.Ignored.

Real

static

Declares that a member runs for real on purpose. Declared after a stub or fake, it sends the accesses it matches to the real member; leak detection doesn't warn about the accesses it covers. It records nothing, and belongs to the NUnit test or fixture that declares it. Explained in Run members for real.

Every method throws InvalidOperationException outside an NUnit test, or when its selection was not woven.

Static methods
void Method<TResult>(Func<TResult> selection)Runs a method with a result, an operator or a conversion for real: () => Random.Range(0, 10).
void Method(Action selection)Runs a void method for real.
void Method<TInstance, TResult>(Func<TInstance, TResult> selection)Runs a method with a result for real, on any instance.
void Method<TInstance>(Action<TInstance> selection)Runs a void method for real, on any instance.
void Constructor<TResult>(Func<TResult> selection)Runs a constructor for real.
void Field<TValue>(Func<TValue> selection)Reads and writes a field for real.
void Field<TInstance, TValue>(Func<TInstance, TValue> selection)Reads and writes a field for real, on any instance.
void Property<TValue>(Func<TValue> selection)Reads and writes a property for real: () => Physics.gravity.
void Property<TInstance, TValue>(Func<TInstance, TValue> selection)Reads and writes a property for real, on any instance.
void Indexer<TValue>(Func<TValue> selection)Reads and writes an element written with [] for real.
void Indexer<TInstance, TValue>(Func<TInstance, TValue> selection)Reads and writes an element written with [] for real, on any array or indexed object.
void Event(Action selection)Runs an event for real, named by a subscription of Arg.Ignored: () => Application.logMessageReceived += Arg.Ignored. Subscriptions reach the real event, and raises invoke its real handlers.
void Event<TInstance>(Action<TInstance> selection)Runs an event for real, on any instance.

Matchers

Arg

static

Matchers for the arguments of a selection. Any other argument matches as NUnit's Is.EqualTo does. Understudy generates it as an internal class into each assembly that references Understudy, such as your test assembly, with the Ignored overloads its events need.

Static methods
ref T Any<T>()Matches every value. In an out or ref position, it stands for the argument and never filters. Throws InvalidOperationException when called anywhere but inside a selection.
ref T That<T>(IResolveConstraint constraint)Matches the values that satisfy an NUnit constraint, such as Arg.That<float>(Is.GreaterThan(50f)). Throws InvalidOperationException when called anywhere but inside a selection.
void Ignored()Names an event in an Event selection, as the right side of += or -=: Fake.Event(() => player.Died += Arg.Ignored). It fits a void delegate with up to 16 parameters, and Understudy generates the overloads an event with a result or with out, ref or in parameters needs. It never runs. Throws InvalidOperationException when a delegate made from it outside a selection is invoked.
TResult Ignored<TResult>()Names an event whose delegate returns a value and takes no parameters: () => bank.Asked += Arg.Ignored<bool>. C# infers type arguments from a delegate's parameters only, so the return type is written out. Throws InvalidOperationException when a delegate made from it outside a selection is invoked.
TResult Ignored<T1, TResult>(T1 a)Names an event whose delegate returns a value, with every type written out: Arg.Ignored<int, float>. Plain Arg.Ignored fits such an event; this form is for a second event in the assembly whose delegate has the same parameters and another return type. Overloads take up to 16 parameters. Throws InvalidOperationException when a delegate made from it outside a selection is invoked.

Leak detection

NonDeterministicAttribute

attr

Declares a member non-deterministic: its result differs between runs, machines or frames, so leak detection warns when a test reaches it for real. Every overload of the name is covered, and the declaration applies to the whole project, from any assembly that references Understudy: [assembly: NonDeterministic(typeof(ServerClock), nameof(ServerClock.Now))].

Members
NonDeterministicAttribute(Type type, string member)Declares the members named member on type non-deterministic.
Type Type { get; }The type that declares the member.
string Member { get; }The member's name.