PageRenderTime 22ms CodeModel.GetById 2ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/Source/Bifrost.Tests/Events/EventDispatcherTests.cs

#
C# | 80 lines | 67 code | 13 blank | 0 comment | 2 complexity | d690a39ef5fc6d8f26ab48d0ff9d2259 MD5 | raw file
 1using System.Collections.Generic;
 2using Bifrost.Events;
 3using Moq;
 4using NUnit.Framework;
 5
 6namespace Bifrost.Tests.Events
 7{
 8	[TestFixture]
 9	public class EventDispatcherTests
10	{
11		[Test]
12		public void PublishingSingleEventShouldPublishEventToRegisteredBus()
13		{
14			var busMock = new Mock<IEventBus>();
15			var dispatcher = new EventDispatcher();
16			var eventMock = new Mock<IEvent>();
17			dispatcher.RegisterBus(busMock.Object);
18			dispatcher.Publish(eventMock.Object);
19			
20			busMock.Verify(b => b.Publish(It.IsAny<IEvent>()), Times.Once());
21		}
22
23		[Test]
24		public void PublishingSingleShouldPublishEventToAllRegisteredBuses()
25		{
26			var firstBusMock = new Mock<IEventBus>();
27			var secondBusMock = new Mock<IEventBus>();
28			var dispatcher = new EventDispatcher();
29			var eventMock = new Mock<IEvent>();
30			dispatcher.RegisterBus(firstBusMock.Object);
31			dispatcher.RegisterBus(secondBusMock.Object);
32			dispatcher.Publish(eventMock.Object);
33
34			firstBusMock.Verify(b => b.Publish(It.IsAny<IEvent>()), Times.Once());
35			secondBusMock.Verify(b => b.Publish(It.IsAny<IEvent>()), Times.Once());
36		}
37
38		[Test]
39		public void PublishingMultipleEventsShouldPublishEventsToRegisteredBus()
40		{
41			var busMock = new Mock<IEventBus>();
42			var dispatcher = new EventDispatcher();
43
44			var events = new List<IEvent>();
45			for( var i=0; i<10; i++ )
46			{
47				var eventMock = new Mock<IEvent>();
48				events.Add(eventMock.Object);
49			}
50			
51			dispatcher.RegisterBus(busMock.Object);
52			dispatcher.Publish(events);
53
54			busMock.Verify(b => b.Publish(It.IsAny<IEnumerable<IEvent>>()), Times.Once());
55		}
56
57		[Test]
58		public void PublishingMultipleEvennntsShouldPublishEventsToAllRegisteredBuses()
59		{
60			var firstBusMock = new Mock<IEventBus>();
61			var secondBusMock = new Mock<IEventBus>();
62			var dispatcher = new EventDispatcher();
63			dispatcher.RegisterBus(firstBusMock.Object);
64			dispatcher.RegisterBus(secondBusMock.Object);
65			var events = new List<IEvent>();
66			for (var i = 0; i < 10; i++)
67			{
68				var eventMock = new Mock<IEvent>();
69				events.Add(eventMock.Object);
70			}
71
72
73			dispatcher.Publish(events);
74
75			firstBusMock.Verify(b => b.Publish(It.IsAny<IEnumerable<IEvent>>()), Times.Once());
76			secondBusMock.Verify(b => b.Publish(It.IsAny<IEnumerable<IEvent>>()), Times.Once());
77		}
78
79	}
80}