如何在 C# 的构造函数中模拟一个变量被初始化的类?

问题描述 投票:0回答:0

我有一个下面的类,我正在尝试为在 C# 中使用 Moq 框架编写单元测试。

public class CustomerClient : ICustomerClient
{
    private readonly ILoggingService _loggingService;
    private readonly ILoggingServiceConfigProvider _loggingServiceConfigProvider;
    private readonly string _customerEndpoint;
    private readonly IHttpPolicy _policy;
    private readonly IHttpClientWrapper _httpClientWrapper;
    private const string LOGGING_TITLE = "service";
    private const string SERVICE_NAME = "service";

    public CustomerClient(string customerEndpoint, ILoggingService loggingService, IConfigurationManager configManager,
        ILoggingServiceConfigProvider loggingServiceConfigProvider)
    {
        _customerEndpoint = customerEndpoint ?? throw new ArgumentNullException(nameof(cpsEndpoint));
        _loggingService = loggingService ?? throw new ArgumentNullException(nameof(loggingService));
        _loggingServiceConfigProvider = loggingServiceConfigProvider ??
                                        throw new ArgumentNullException(nameof(loggingServiceConfigProvider));
        _customerEndpoint = customerEndpoint;
        _policy = new HttpPolicyBuilderAsync(configManager, _loggingService, "customerPolicyOptions.json", LOGGING_TITLE);
        _httpClientWrapper = new HttpClientWrapper(_cpsEndpoint, loggingService, SERVICE_NAME, _loggingServiceConfigProvider);
    }

    public CustomerInfo GetData(int rosterId, IList<string> leagues, IList<string> teams, LoggingContext loggingContext)
    {
      //....
    }
}

这里是

HttpPolicyBuilderAsync
构造函数

public HttpPolicyBuilderAsync(IConfigurationManager configManager, ILoggingService loggingService, string fileName, string svcName) : base(configManager, loggingService, fileName)
{
    _configurationStateManager = new ConfigurationStateManager<HttpPolicyContainer>(configManager, fileName, RebuildConfigObjects, loggingService);
    _svcName = svcName ?? throw new ArgumentNullException(nameof(svcName));
}

public void EnsureValidStatusCode(HttpStatusCode statusCode)
{
    if (_configurationStateManager.GetState().RetryHttpStatusCodes.Contains(statusCode))
    {
        throw new HttpRequestExceptionWithStatusCode($"Status code: {statusCode} to retry for config: {_fileName}", statusCode);
    }
}

我想出了下面的代码,但是当我运行单元测试时,我得到一个异常,因为我在

_policy
的构造函数中有一个
CustomerClient
这给了我一个错误,因为我现在还没有嘲笑它.我怎样才能模拟它,以便当我调用
CustomerClient
构造函数时,它已经为我初始化了。

[TestFixture]
public class CustomerClientTests
{
    private Mock<ILoggingService> _loggingServiceMock;
    private Mock<IConfigurationManager> _configManagerMock;
    private Mock<ILoggingServiceConfigProvider> _loggingServiceConfigProviderMock;
    private Mock<IHttpClientWrapper> _httpClientWrapperMock;
    private CustomerClient _customerClient;

    [SetUp]
    public void Setup()
    {
        _loggingServiceMock = new Mock<ILoggingService>();
        _configManagerMock = new Mock<IConfigurationManager>();
        _loggingServiceConfigProviderMock = new Mock<ILoggingServiceConfigProvider>();
        _httpClientWrapperMock = new Mock<IHttpClientWrapper>();
        // exception is thrown once this is called
        _customerClient = new CustomerClient("endpoint",_loggingServiceMock.Object, _configManagerMock.Object, _loggingServiceConfigProviderMock.Object);
    }

    [Test]
    public void CustomerClient_GetData_ShouldReturnEmptyRoster_WhenResponseStatusCodeIsNotOK()
    {
      ....
    }
}
c# moq
© www.soinside.com 2019 - 2024. All rights reserved.