diff --git a/encompass-cs/MessageManager.cs b/encompass-cs/MessageManager.cs index d22f2c0..b6a758d 100644 --- a/encompass-cs/MessageManager.cs +++ b/encompass-cs/MessageManager.cs @@ -28,7 +28,9 @@ namespace Encompass internal IEnumerable GetMessagesByType() where TMessage : struct, IMessage { - return messageTypeToMessages[typeof(TMessage)].Cast(); + return messageTypeToMessages.ContainsKey(typeof(TMessage)) ? + messageTypeToMessages[typeof(TMessage)].Cast() : + Enumerable.Empty(); } } } diff --git a/test/EngineTest.cs b/test/EngineTest.cs index 069c49f..c38a0ae 100644 --- a/test/EngineTest.cs +++ b/test/EngineTest.cs @@ -227,6 +227,29 @@ namespace Tests } } + static IEnumerable emptyReadMessagesResult; + [Reads(typeof(MockMessage))] + class ReadMessagesWhenNoneExistEngine : Engine + { + public override void Update(float dt) + { + emptyReadMessagesResult = ReadMessages(); + } + } + + [Test] + public void ReadMessagesWhenNoneHaveBeenEmitted() + { + var worldBuilder = new WorldBuilder(); + worldBuilder.AddEngine(); + + var world = worldBuilder.Build(); + + world.Update(0.01f); + + emptyReadMessagesResult.Should().BeEmpty(); + } + [Test] public void EmitUndeclaredMessage() {