由于使用 xunit 的保护级别 .net,无法访问方法 'Handle' MediatR

Cannot access method 'Handle' MediatR due to its protection level .net using xunit

我想测试由于 MediatR 中的 AsyncRequestHandler 接口而受到保护的处理程序。这是我的处理程序:

public sealed class AddUserDeviceCommandHandler : AsyncRequestHandler<AddUserDeviceCommand>
{
    private readonly ICurrentUserService _userService;
    private readonly IRepository<UserDevice> _repository;

    public AddUserDeviceCommandHandler(ICurrentUserService userService, IRepositoryAccessor repositoryAccessor)
    {
        _userService = userService;
        _repository = repositoryAccessor.GetRepository<UserDevice>(_userService.CustomerIsin,
            reThrowException: true, type: DatabaseType.Raven);
    }

    protected override async Task Handle(AddUserDeviceCommand request, CancellationToken cancellationToken)
    {

        //do do do some code
        await _repository.AddOrUpdateAsync(model);
    }
}

如您所见,Handle 方法受到保护。因此在我的测试中我无法访问 handle 方法:

AddUserDeviceCommand command = new AddUserDeviceCommand(data.AppBuildNo,data.Width,data.Height,data.PlatformInfo,data.DevicePlatform);
        AddUserDeviceCommandHandler handler = new AddUserDeviceCommandHandler(userservice.Object, repoacc.Object);

        handler.?????

感谢任何帮助

handler 变量声明为 IRequestHandler<AddUserDeviceCommand> 类型。

AsyncRequestHandler<TRequest> 实现了 IRequestHandler<TRequest>,通过它你可以访问它的 Handle 方法。

AddUserDeviceCommand command = new AddUserDeviceCommand(  
    ​data.AppBuildNo, data.Width, data.Height, data.PlatformInfo, data.DevicePlatform
    );
IRequestHandler<AddUserDeviceCommand> handler = new AddUserDeviceCommandHandler(
    userservice.Object, repoacc.Object
    );

await handler.Handle(command, CancellationToken.None);