using System.Threading.Tasks; using Shouldly; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities.Events; using Xunit; namespace Volo.Abp.EventBus { public class EventBus_EntityEvents_Test : EventBusTestBase { [Fact] public void Should_Call_Created_And_Changed_Once() { var handler = new MyEventHandler(); EventBus.Register>(handler); EventBus.Register>(handler); var asyncHandler = new MyAsyncEventHandler(); EventBus.AsyncRegister>(asyncHandler); EventBus.AsyncRegister>(asyncHandler); EventBus.Trigger(new EntityCreatedEventData(new MyEntity())); handler.EntityCreatedEventCount.ShouldBe(1); handler.EntityChangedEventCount.ShouldBe(1); asyncHandler.EntityCreatedEventCount.ShouldBe(1); asyncHandler.EntityChangedEventCount.ShouldBe(1); } public class MyEntity : Entity { } public class MyEventHandler : IEventHandler>, IEventHandler> { public int EntityChangedEventCount { get; set; } public int EntityCreatedEventCount { get; set; } public void HandleEvent(EntityChangedEventData eventData) { EntityChangedEventCount++; } public void HandleEvent(EntityCreatedEventData eventData) { EntityCreatedEventCount++; } } public class MyAsyncEventHandler : IAsyncEventHandler>, IAsyncEventHandler> { public int EntityChangedEventCount { get; set; } public int EntityCreatedEventCount { get; set; } public Task HandleEventAsync(EntityChangedEventData eventData) { EntityChangedEventCount++; return Task.FromResult(0); } public Task HandleEventAsync(EntityCreatedEventData eventData) { EntityCreatedEventCount++; return Task.FromResult(0); } } } }