Fake fields and properties
Fake a field or property
A fake replaces a field's or property's storage with a stored value of its own. Reads and writes in the code under test reach
the stored value instead of the real member, which keeps its own value. The stored value starts at the
initial argument, or at default without one. Read Value to check what the code under test
wrote, and assign Value to change it during the test:
FakedProperty<float> timeScale = Fake.Property(() => Time.timeScale, initial: 1f);
pauseMenu.Open();
Assert.That(timeScale.Value, Is.EqualTo(0f)); // the real engine was never paused
A get-only property and a readonly field can be faked too. Your test writes their stored value through Value, and writes from the member's own constructor or
private setter also reach it.
A direct read in the test, such as Time.timeScale, reaches the real member, since tests aren't woven:
read Value instead. Code analysis points such a read out.
Fake a singleton
A static field or property holds a singleton, so a test replaces it without changing the code under test:
Fake.Field(() => GameManager.Instance, initial: testManager); // Fake.Property for a property
For a complete example, refer to Replace a singleton.
One stored value per instance
A fake keeps one stored value per instance it reaches. On a table, each instance's stored value starts at the initial value, or at what a factory computes for it:
FakedPropertyTable<Rigidbody, Vector3> velocities = Fake.Property((Rigidbody b) => b.linearVelocity, initial: Vector3.zero);
velocities[boss].Value = Vector3.up; // the boss only
Fake.Field((Enemy e) => e.health, initial: (Enemy e) => e.maxHealth); // each enemy starts full
A selection through a chain, such as () => hero.gun.ammo, keeps a stored value for each gun the hero
holds over the test. Value is the one of the gun held at that moment:
FakedField<int> ammo = Fake.Field(() => hero.gun.ammo, initial: 10);
hero.Equip(rifle); // the rifle starts at 10
ammo.Value = 2; // the rifle only: the pistol keeps its own
When the chain reaches no instance, such as while hero.gun is null, reading
Value throws InvalidOperationException.
Each test starts every fake over at its initial value, also when the fake is declared once for a whole fixture. For more information, refer to Lifetime.
Fake a collection
A fake stores whole values, never one entry per index. To control what a collection holds, fake the field or property that holds it with one your test builds:
Tile[] tiles = { Tile.Floor, Tile.Wall, Tile.Floor };
Fake.Field(() => level.tiles, initial: tiles);
level.Dig(1);
Assert.That(tiles[1], Is.EqualTo(Tile.Floor));
An initial object, such as this array, is the same object in every test and for every instance. To
give each its own, pass a factory instead: initial: () => new List<Item>().
To answer reads of single elements, stub the indexer instead. For more information, refer to Stub members.
Structs changed in place
A fake stores a struct whole. Code that changes one part in place, such as position.x = 5f or
velocity.Normalize(), reads the whole value, changes the copy, and writes it back, so the fake sees each
of those reads and writes:
FakedField<Vector3> position = Fake.Field(() => player.position);
player.Teleport(); // position.x = 5f
Assert.That(position.Value.x, Is.EqualTo(5f));
To set one part from the test, copy the value, change it and assign it back:
Vector3 value = position.Value;
value.x = 5f;
position.Value = value;
ref argument is copied in and out. A ref local, a
ref return or a pointer that holds the member across statements reaches the stored value, but those
accesses aren't recorded.What a fake doesn't change
A fake covers what the code under test sees: a faked transform.position moves nothing for physics or rendering.
For everything a fake doesn't change, refer to What a double doesn't reach.