Run members for real
Declare a member real
Real declares that the accesses its selection matches run for real on purpose. It takes the same
selections as Spy, except Catch, and records nothing. It has two uses:
- Leak detection doesn't warn about the member it selects.
- It overrides the stubs and fakes declared before it.
Real.Method(() => Random.Range(Arg.Any<int>(), Arg.Any<int>())); // this test wants real randomness
spawner.SpawnWave();
Go back to the real member
When several declarations match an access, the last one declared decides, and Real is one of them:
declared after a stub or fake, it takes over the accesses it matches:
Stub.Property(() => Time.deltaTime).Returns(0.1f);
player.Tick();
Real.Property(() => Time.deltaTime);
player.Tick(); // real deltaTime
To make the stub decide again, declare it again: calling Returns on it isn't enough, since the
Real stays the last one declared. The overridden stub keeps recording what it matches, as a spy does.
After a Real.Property or Real.Field, a fake keeps its stored value but no longer serves those
accesses; after a Real.Event, subscriptions reach the real event.
Make part of a stub real
A narrow Real after a broad stub keeps some calls real:
// Every raycast misses, except downward ones.
Stub.Method(() => Physics.Raycast(Arg.Any<Vector3>(), Arg.Any<Vector3>(), Arg.Any<float>())).Returns(false);
Real.Method(() => Physics.Raycast(Arg.Any<Vector3>(), Vector3.down, Arg.Any<float>()));
Lift a fixture's fake for one test
A Real in a test ends with the test, so the fixture's fake applies again in the next one:
[OneTimeSetUp]
public void Quiet() => Fake.Event(() => Application.logMessageReceived += Arg.Ignored);
[Test]
public void ShowsErrors()
{
Real.Event(() => Application.logMessageReceived += Arg.Ignored);
console.Bind();
}