admin/src/Oms.Tests/Messaging/OrderBatchJobOutboxPublisherTests.cs

66 lines
2.4 KiB
C#

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<OmsOutboxTestModule>
{
private readonly IUnitOfWorkManager _uowManager;
private readonly RecordingPhysicalDistributedEventBus _physicalBus;
private readonly IDistributedEventBus _eventBus;
private readonly IEfDbContextProvider<IntegrationEventLogContext> _dbProvider;
protected override void SetApplicationCreationOptions(FakeApplicationCreationOptions options)
{
options.UseAutofac();
}
public OrderBatchJobOutboxPublisherTests()
{
_uowManager = ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
_eventBus = ServiceProvider.GetRequiredService<IDistributedEventBus>();
_physicalBus = ServiceProvider.GetRequiredService<RecordingPhysicalDistributedEventBus>();
_dbProvider = ServiceProvider.GetRequiredService<IEfDbContextProvider<IntegrationEventLogContext>>();
}
[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<OrderBatchJobIntegrationEvent>().Subject;
evt.Action.Should().Be(OrderBatchActionCodes.ForecastBilling);
evt.OrderId.Should().Be(55);
evt.MessageId.Should().Be(message.MessageId);
}
}