using Fake; using Fake.EntityFrameworkCore; using Fake.EventBus.Distributed; using Fake.EventBus.IntegrationEventLog; using Fake.Testing; using Fake.UnitOfWork; using FluentAssertions; using Microsoft.Extensions.DependencyInjection; using Oms.Application.Messaging; using Oms.Domain.OrdersAggregate.Consts; using Oms.Tests.Messaging.Fakes; namespace Oms.Tests.Messaging; public class OrderBatchJobOutboxPublisherTests : ApplicationTestWithTools { private readonly IUnitOfWorkManager _uowManager; private readonly RecordingPhysicalDistributedEventBus _physicalBus; private readonly IDistributedEventBus _eventBus; private readonly IEfDbContextProvider _dbProvider; protected override void SetApplicationCreationOptions(FakeApplicationCreationOptions options) { options.UseAutofac(); } public OrderBatchJobOutboxPublisherTests() { _uowManager = ServiceProvider.GetRequiredService(); _eventBus = ServiceProvider.GetRequiredService(); _physicalBus = ServiceProvider.GetRequiredService(); _dbProvider = ServiceProvider.GetRequiredService>(); } [Fact] public async Task ForecastBilling任务消息_事务Commit后投递() { _physicalBus.Clear(); var message = new OrderBatchJobMessage { JobId = 1, JobItemId = 2, Action = OrderBatchActionCodes.ForecastBilling, OrderId = 55, MessageId = "job:1:55:forecast-billing", OperatorUserId = 9, PayloadJson = "{\"CouponInstanceId\":1}" }; using (var uow = _uowManager.Begin(OmsOutboxUnitOfWork.Transactional())) { await _dbProvider.GetDbContextAsync(); await _eventBus.PublishAsync(OrderBatchJobIntegrationEvent.FromMessage(message)); _physicalBus.Published.Should().BeEmpty(); await uow.CompleteAsync(); } var evt = _physicalBus.Published.Should().ContainSingle() .Which.Should().BeOfType().Subject; evt.Action.Should().Be(OrderBatchActionCodes.ForecastBilling); evt.OrderId.Should().Be(55); evt.MessageId.Should().Be(message.MessageId); } }