我正在尝试测试masstransit消费者 我的消费者看起来像这样:
public class FilePolicyEvaluationConsumer : IFilePolicyEvaluationConsumer
{
public readonly IFilePolicyEvaluationCore _filePolicyEvaluationCore;
public readonly IRepositoryClient _repositoryClient;
public readonly IFPRepository _fPRepository;
public FilePolicyEvaluationConsumer()
{
}
public FilePolicyEvaluationConsumer(IFilePolicyEvaluationCore filePolicyEvaluationCore, IRepositoryClient repositoryClient, IFPRepository fPRepository)
{
_filePolicyEvaluationCore = filePolicyEvaluationCore;
_repositoryClient = repositoryClient;
_fPRepository = fPRepository;
}
public Task Consume(ConsumeContext<InvokePolicyOnFileMessage> context)
{
return Task.Run( async () => {
Guid fileId = context.Message.FileId;
FileMetadata fileMetaData = await _repositoryClient.GetFileMetadata(fileId);
_filePolicyEvaluationCore.Run(fileMetaData, context.Message.CorrelationId);
});
}
但是当我像这样配置测试时
_harness = new InMemoryTestHarness();
FilePolicyEvaluationConsumer filePolicy = new FilePolicyEvaluationConsumer();
_consumer = _harness.Consumer<FilePolicyEvaluationConsumer>();
await _harness.Start();
await _harness.InputQueueSendEndpoint.Send(message1);
当我在测试中输入消费者的所有对象_filePolicyEvaluationCore,_repositoryClient,_fPRepository在消费者中为空时,我该怎样才能注入这个对象?
答案 0 :(得分:2)
它是null
,因为您可能在生产代码中使用了一个容器并注入了构造函数参数。 MassTransit没有内置注入,因为它不使用任何容器。
您可以使用消费者工厂重载来使其工作:
_harness = new InMemoryTestHarness();
var filePolicy = new FilePolicyEvaluationConsumer();
_consumer = _harness.Consumer(
() => new FilePolicyEvaluationConsumer(filePolicy));