【发布时间】:2022-01-20 04:15:02
【问题描述】:
我正在尝试针对 MediatR 命令进行集成测试,该命令的处理程序依赖于注入到其构造函数中的 IRequestClient。
public class SayHelloCommand : IRequest<string>
{
}
public class SayHelloCommandHandler : IRequestHandler<SayHelloCommand, string>
{
private readonly IRequestClient<IGetProfileMessageResult> _profileClient;
public SayHelloCommandHandler(IRequestClient<IGetProfileMessageResult> profileClient)
{
_profileClient = profileClient;
}
public async Task<string> Handle(SayHelloCommand request, CancellationToken cancellationToken)
{
var profile = (await _profileClient.GetResponse<IGetProfileMessageResult>(new {ProfileId = 1})).Message;
return $"Hello {profile.FirstName}";
}
}
我已将我的测试套件设置为使用 InMemoryMassTransit,但每当我运行测试时,它会在使用 IRequestClient 到达调用时超时。我也尝试过最小化 IRequestClient 以返回这样的默认响应 -
[Test]
public async Task ShouldSayHello()
{
var mockRequestClient = new Mock<IRequestClient<IGetProfileMessageResult>>();
mockRequestClient.Setup(x => x.GetResponse<IGetProfileMessageResult>(It.IsAny<Object>(), default, default)
.Result.Message).Returns(new GetProfileMessageResult
{
FirstName = "John"
});
serviceCollection.Add(new ServiceDescriptor(typeof(IRequestClient<IGetProfileMessageResult>), mockRequestClient.Object));
var result = await SendAsync(command);
result.Status.Should().BeFalse();
result.Message.Should().Contain("John");
}
但这仍然超时。
有没有办法可以设置 InMemoryMassTransit 在调用 requestclient 时返回默认响应?
【问题讨论】:
标签: .net-core nunit moq masstransit