Skip to content

Instantly share code, notes, and snippets.

@scionwest
Created January 18, 2015 01:52
Show Gist options
  • Save scionwest/6619f6ac183a7bf625f9 to your computer and use it in GitHub Desktop.
Save scionwest/6619f6ac183a7bf625f9 to your computer and use it in GitHub Desktop.
public void Publish_invokes_callbacks()
{
bool callbackCalled = false;
string messageContent = "Test";
var notificationCenter = new NotificationManager();
notificationCenter.Subscribe<ShoutMessage>((msg, sub) =>
{
if (msg.Content == messageContent)
{
callbackCalled = true;
}
});
// Act
notificationCenter.Publish(new ShoutMessage(messageContent));
// Assert
Assert.IsTrue(callbackCalled);
}
[TestMethod]
[TestCategory("Runtime.Game - NotificationManager")]
public void Handler_can_unsubscribe()
{
var notificationCenter = new NotificationManager();
int callCount = 0;
// Build our notification.
ISubscription subscriber = notificationCenter.Subscribe<MessageFixture>(
(message, sub) => callCount++);
// Subscribe our notification and publish a new message
notificationCenter.Publish(new MessageFixture("Test"));
// Act
// Unsubscribe the notification and attempt a new publish
subscriber.Unsubscribe();
notificationCenter.Publish(new MessageFixture("Test"));
// Assert
Assert.AreEqual(1, callCount, "The callbacks were not fired properly");
}
[TestMethod]
[TestCategory("Runtime.Game - NotificationManager")]
public void Handler_receives_only_its_message()
{
// Arrange
// Set up the first handler
var notificationCenter = new NotificationManager();
notificationCenter.Subscribe<MessageFixture>(
(message, sub) => ExceptionFactory
.ThrowIf<InvalidOperationException>(message.GetType() != typeof(MessageFixture)));
notificationCenter.Subscribe<SecondaryMessageFixture>(
(message, sub) => ExceptionFactory
.ThrowIf<InvalidOperationException>(message.Content.GetType() != typeof(DefaultPlayer)));
// Act
notificationCenter.Publish(new MessageFixture("Test"));
notificationCenter.Publish(new SecondaryMessageFixture(new DefaultPlayer(null)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment