Fake events
Name an event
Outside its class, C# only lets you write an event before += or -=. A selection
names an event the same way, with Arg.Ignored on the right:
Fake.Event(() => SceneManager.sceneLoaded += Arg.Ignored);
Arg.Ignored fits every event except one whose delegate returns a value and takes no parameters,
such as Func<bool>. For it, write the return type: Arg.Ignored<bool>. Code analysis
asks for it, with a fix.
Fake an event
An event fake replaces the event's storage. Subscriptions in the code under test land in the fake and never reach the real
event, and when the code under test raises the event, the fake's handlers run. It starts with no handlers. An event the engine
raises, such as Application.logMessageReceived, never reaches the fake's handlers:
// User handlers never see the engine's own logs.
Fake.Event(() => Application.logMessageReceived += Arg.Ignored);
Declare the fake before the code under test subscribes: a handler subscribed earlier stays on the real event, where the
fake's Raise doesn't reach it.
Raise an event from the test
Raise invokes the handlers the code under test subscribed to the fake, and returns what the invocation
returns:
FakedEvent sceneLoaded = Fake.Event(() => SceneManager.sceneLoaded += Arg.Ignored);
hud.Init();
sceneLoaded.Raise(scene, LoadSceneMode.Single);
Assert.That(hud.Title, Is.EqualTo("Level 1"));
Its arguments must fit the event's delegate; code analysis checks them. With no handler subscribed, it does nothing. A handler the test subscribes itself goes to the real event, since the test isn't woven: let the code under test subscribe.
Fake an event on every instance
On a table, each instance's fake has its own handlers.
EachRaise raises the event on every instance:
FakedEventTable<Enemy> killed = Fake.Event((Enemy enemy) => enemy.Killed += Arg.Ignored);
wave.Spawn();
killed[wave.Enemies[2]].Raise(50); // that enemy's handlers only
killed.EachRaise(10); // every enemy's handlers
Check handlers and raises
An event spy or fake has two lists. Handlers holds the handlers the code under test subscribed after the double
was created and hasn't unsubscribed. Invocations holds the raises the code under test made:
SpiedEvent died = Spy.Event(() => player.Died += Arg.Ignored);
hud.Show();
hud.Hide();
Assert.That(died.Handlers, Is.Empty); // the HUD unsubscribed when hidden
SpiedEvent changed = Spy.Event(() => score.Changed += Arg.Ignored);
hud.Bind(score);
score.Add(10);
Assert.That(changed.Invocations[0].Arguments[0], Is.EqualTo(10));
Handlers subscribed before the spy existed are unknown to it. A raise is recorded even with no handler subscribed, but without its arguments, which C# never
evaluates then: HadHandlers is false, and reading Arguments throws. To check the arguments, subscribe a handler first.
A raise from the test with Raise isn't an invocation: only what the code under test does is recorded.
Delegate fields and UnityEvents
A field or property of a delegate type isn't an event. Fake.Field or Fake.Property stores
what it holds, and the test invokes it through Value. A Method selection on its
Invoke records its invocations:
SpiedMethod deathInvoked = Spy.Method(() => player.OnDeath.Invoke(Arg.Any<int>()));
A UnityEvent isn't an event: select its AddListener and Invoke as methods, such
as
() => button.onClick.AddListener(Arg.Any<UnityAction>()). To check that an Inspector-wired
listener runs, refer to Calls from the engine.