Skip to content

Instantly share code, notes, and snippets.

@cmeeren
Created April 2, 2017 09:49
Show Gist options
  • Select an option

  • Save cmeeren/467d8294bafbcb18af1268e5967b6128 to your computer and use it in GitHub Desktop.

Select an option

Save cmeeren/467d8294bafbcb18af1268e5967b6128 to your computer and use it in GitHub Desktop.
Tests that all handlers are called last with the latest state (fails)
namespace NestedEventTest
{
using System;
using NUnit.Framework;
public class EventSource
{
private int i;
public event Action<int> Event;
public void TriggerEvent()
{
this.Event?.Invoke(++this.i);
}
}
public class EventSourceTests
{
[Test]
public void When_InvokingFromHandler_Should_InvokeAllWithLatestState()
{
// Arrange
var listener1InvokedState = 0;
var listener2InvokedState = 0;
var listener3InvokedState = 0;
var sut = new EventSource();
Action<int> listener1 = null;
Action<int> listener2 = null;
Action<int> listener3 = null;
listener1 = i => listener1InvokedState = i;
listener2 = i =>
{
listener2InvokedState = i;
if (i == 1) sut.TriggerEvent();
};
listener3 = i => listener3InvokedState = i;
sut.Event += listener1;
sut.Event += listener2;
sut.Event += listener3;
// Act
sut.TriggerEvent();
// Assert
Assert.That(listener1InvokedState, Is.EqualTo(2));
Assert.That(listener2InvokedState, Is.EqualTo(2));
Assert.That(listener3InvokedState, Is.EqualTo(2)); // Fails, is 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment