如何使用Moq模拟NserviceBus SendLocal

时间:2018-10-30 03:28:46

标签: nservicebus

  

context.Verify引发异常:对象引用未设置为对象的实例

var context = new Mock<IMessagehandlerContext>();

context.Setup(x => x.SendLocal(It.IsAny<object>()))
.Returns(Task.CompletedTask);

context.Verify(b => b.SendLocal(It.IsAny<objec>()), Times.Exactly(1))

1 个答案:

答案 0 :(得分:0)

建议您不要使用NServiceBus.Testing nuget包中的TestableMessageHandlerContext,而不是模拟IMessageHandlerContext。查看文档here

如何使用它的示例如下:

[Test]
public async Task ShouldReplyWithResponseMessage()
{
  var handler = new MyReplyingHandler();
  var context = new TestableMessageHandlerContext();

  await handler.Handle(new MyRequest(), context)
    .ConfigureAwait(false);

  Assert.AreEqual(1, context.RepliedMessages.Length);
  Assert.IsInstanceOf<MyResponse>(context.RepliedMessages[0].Message);
}