IConfiguration 在模拟时不断获得空值

IConfiguration keeps getting null value when mocking

请问我是否正确地模拟了我的 IConfiguration,因为我一直收到空错误。我试图在读取 appsettings.json.

的地方模拟我的控制器

ControllerOne.cs

 private readonly IConfiguration _configuration;
 public Controller(IConfiguration configuration)
 {
     _configuration = configuration;
 }

 [HttpPost("upload")]
 public async Task<IActionResult> UploadFile([FromForm(Name = "files")] List<IFormFile> files, CancellationToken cancellationToken = default)
 {
      string directory = _configuration.GetValue<string>("ShippingService:ServerDirectory");
      string subDirectory = _configuration.GetValue<string>("ShippingService:UploadRateSubDirectory");
 }

ControllerTest.cs

public class ControllerTest: ControllerTestsBase<ControllerOne>
{

   [Fact]
   public void TestOne()
   {
       IFormFile file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt");
       List<IFormFile> formFiles = new List<IFormFile>();
       formFiles.Add(file);
       //when
       await Controller.UploadFile(formFiles, default);
   }
}

ControllerTestsBase.cs

 public abstract class ControllerTestsBase<T>
    where T : ApiControllerBase
{
    protected readonly T Controller;
    protected readonly AutoMocker Mocker;

    protected ControllerTestsBase()
    {
        Mocker = new AutoMocker();

        var httpResponseMock = Mocker.GetMock<HttpResponse>();
        httpResponseMock.Setup(mock => mock.Headers).Returns(new HeaderDictionary());

        var httpRequestMock = Mocker.GetMock<HttpRequest>();

        var httpContextMock = Mocker.GetMock<HttpContext>();
        httpContextMock.Setup(mock => mock.Response).Returns(httpResponseMock.Object);
        httpContextMock.Setup(mock => mock.Request).Returns(httpRequestMock.Object);

        var fakeValues = new Dictionary<string, string>
        {
            {"ShippingService:ServerDirectory", "abc"},
            {"ShippingService:UploadRateSubDirectory", "test.com"}
        };

        IConfiguration config = new ConfigurationBuilder()
            .AddInMemoryCollection(fakeValues)
            .Build();

        Controller = Mocker.CreateInstance<T>();
        Controller.ControllerContext.HttpContext = httpContextMock.Object;
    }
}

调试跟踪到Controller.csmethodOne时,_configuration一直提示null错误

docs状态:

If you have a special instance that you need to use, you can register it with .Use(...). This is very similar to registrations in a regular IoC container (i.e. For().Use(x) in StructureMap).

因此我希望这会起作用:

IConfiguration config = new ConfigurationBuilder()
    .AddInMemoryCollection(fakeValues)
    .Build();

Mocker.Use<IConfiguration>(config);

Controller = Mocker.CreateInstance<T>();
Controller.ControllerContext.HttpContext = httpContextMock.Object;