我是MassTransit和消息传递的新手。我正在尝试为IBus.Publish
编写一个单元测试,但结果不能成功。
我正在观察RabbitMQ的错误,我的观察者看起来像这样:
public class FaultObserver : IReceiveObserver
{
public FaultObserver(IRequestUpdater statusUpdater,Lazy<IBus> bus)
{
this.statusUpdater = statusUpdater;
this.bus = bus;
}
public async Task ConsumeFault<T>(ConsumeContext<T> context, TimeSpan duration, string consumerType, Exception exception) where T : class
{
}
}
我的测试看起来像下面的代码
var bus = fixture.Freeze<Mock<IBus>>();
bus.Setup(bu => bu.Publish<ReportFailedEvent>(It.IsAny<ReportFailedEvent>(),It.IsAny<CancellationToken>())).Verifiable();
var sut = fixture.Create<ReportRequestedFaultObserver>();
// Act
await sut.ConsumeFault(context.Object,new TimeSpan(0,0,1),string.Empty,exception);
// Assert
//bus.Verify(b => b.Publish<ReportFailedEvent>(It.IsAny<ReportFailedEvent>(), It.IsAny<CancellationToken>()), Times.Exactly(1));
bus.Verify(b =>b.Publish<ReportFailedEvent>(new ReportFailedEvent(request,exception.Message),It.IsAny<CancellationToken>()),Times.Once());
我的设置看起来像
[SetUp]
public void SetUp()
{
fixture.Customize(new AutoMoqCustomization());
var inMemoryTransportCache = new InMemoryTransportCache(Environment.ProcessorCount);
bus = Bus.Factory.CreateUsingInMemory(configure =>
{
configure.SetTransportProvider(inMemoryTransportCache);
configure.ReceiveEndpoint("input_queue", configurator =>
{
configurator.Handler<(cc =>
{
});
});
});
bus.Start().Ready.Wait();
}
我无法模仿我的Publish
方法。有谁知道我做错了什么?
答案 0 :(得分:0)
您的公交模拟车是错误的。应该是:
bus.Setup(bu => bu.Publish<ReportFailedEvent>(
It.IsAny<object>(), It.IsAny<CancellationToken>()));
按照MassTransit中的接口定义,发布采用object
和CancellationToken
:
Task Publish(object message, CancellationToken cancellationToken = default(CancellationToken));
此外,如果要检查邮件的内容,可以使用Moq Callback
扩展名:
ReportFailedEvent message = null;
bus
.Setup(bu => bu.Publish<ReportFailedEvent>(It.IsAny<object>(), It.IsAny<CancellationToken>()))
.Callback<object, CancellationToken>((a, b) => { message = a as ReportFailedEvent; });
//.. your system under test code....
Assert.That(message.Property, Is.EqualTo(expectation));
如果要设置优先级,MassTransit界面如下所示:
public static Task Publish<T>(
this IPublishEndpoint endpoint,
T message,
Action<PublishContext<T>> callback,
CancellationToken cancellationToken = default(CancellationToken))
where T : class;
一个例子是:
bus.Publish<ReportFailedEvent>(message, context =>
{
context.SetPriority(messagePriority.Priority);
});
随附的起订量为:
ReportFailedEvent message = null;
bus
.Setup(bu => bu.Publish<ReportFailedEvent>(It.IsAny<object>(), It.IaAny<Action<PublishContext<ReportFailedEvent>>>(), It.IsAny<CancellationToken>()))
.Callback<object, Action<PublishContext<ReportFailedEvent>>, CancellationToken>((a, b, c) => { message = a as ReportFailedEvent; });
请注意,理想情况下,您应该发布接口而不是类,所以用IReportFailedEvent
而不是ReportFailedEvent
。