0

我正在尝试使用PactNet以下方法编写合同测试:

public async Task<IEnumerable<Models.RefData.Instrument> GetInstruments(string issuerCountry, string instrumentType)
{
    ValidateNotNullOrWhiteSpaceParameter(issuerCountry, nameof(issuerCountry));
    ValidateNotNullOrWhiteSpaceParameter(instrumentType, nameof(instrumentType)); ;

    var queryString = $"instruments?issuerCountry={HttpUtility.UrlEncode(issuerCountry)}&instrumentType={HttpUtility.UrlEncode(instrumentType)}";

    int pageNo = 0;
    int pageSize = 20;

    _logger.LogDebug($"GetInstruments Request:{queryString}");

    var httpResponseMessage = await _client.GetAsync(queryString + $"&page={pageNo}&size={pageSize}");

    _logger.LogDebug($"GetInstruments Response Status Code:{httpResponseMessage.StatusCode}");

    switch (httpResponseMessage.StatusCode)
    {
        case HttpStatusCode.OK:
        var content = await httpResponseMessage.Content.ReadAsStringAsync();
        var result = JsonConvert.DeserializeObject<GetInstrumentsResponse>(content);
                    
        // if there are no results, return Empty
        if (result.Metadata.TotalElements == 0)
        {
            return Enumerable.Empty<Models.RefData.Instrument>();
        }

        var instruments = new List<Models.RefData.Instrument>();
        instruments.AddRange(result.Embedded.Instruments);
        for (pageNo = 1; pageNo < result.Metadata.TotalPages; pageNo++)
        {
            var innerQueryString = queryString + $"&page={pageNo}&size={pageSize}";

            _logger.LogDebug($"GetInstruments Request Additional Page:{innerQueryString}");

            var httpResponseMessage2 = await _client.GetAsync(innerQueryString);
            if (httpResponseMessage2.StatusCode != HttpStatusCode.OK)
            {
                _logger.LogError($"The requested page number {pageNo} gets response error {httpResponseMessage2.StatusCode.ToString()}.");
                throw new UnexpectedResponseException(httpResponseMessage.StatusCode);
            }
                var content2 = await httpResponseMessage2.Content.ReadAsStringAsync();
                var result2 = JsonConvert.DeserializeObject<GetInstrumentsResponse>(content2);

                if (result2.Embedded.Instruments != null && result2.Embedded.Instruments.Any())
                {
                    instruments.AddRange(result2.Embedded.Instruments);
                }
            }

            if (instruments.Count != result.Metadata.TotalElements)
            {
                _logger.LogWarning($"Total number of instruments is different from MetaData.  MetaData states {result.Metadata.TotalElements}, however only {instruments.Count} instruments retrieved.");
        }

        _logger.LogDebug($"GetInstruments Result:{instruments.ToJson()}");

        return instruments;
        default:
            throw new UnexpectedResponseException(httpResponseMessage.StatusCode);
    }
}

我创建了以下内容并将ConsumerPactTests.cs用作指南。ConsumerPactClassFixture.cs

public class ConsumerPactTests : IClassFixture<ConsumerPactClassFixture>
{
    private IMockProviderService _mockProviderService;
    private string _mockProviderServiceBaseUri;

    public ConsumerPactTests(ConsumerPactClassFixture fixture)
    {
        _mockProviderService = fixture.MockProviderService;
        _mockProviderService.ClearInteractions(); //NOTE: Clears any previously registered interactions before the test is run
        _mockProviderServiceBaseUri = fixture.MockProviderServiceBaseUri;
    }

    [Fact]
    public void ItHandlesInvalidDateParam()
    {
        // Arange
        var invalidRequestMessage = "issuerCountry or instrumentType is not valid";
        _mockProviderService.Given("There is data")
            .UponReceiving("A invalid GET request for Date Validation with invalid date parameter")
            .With(new ProviderServiceRequest
            {
                Method = HttpVerb.Get,
                Path = "/api/v2",
                Query = "issuerCountry=USA&instrumentType=foo"
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status = 400,
                Headers = new Dictionary<string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new
                {
                    message = invalidRequestMessage
                }
            });

        // Act
        RefDataHttpService sut = new RefDataHttpServiceBuilder().Build();
        var result = sut.GetInstruments("USA", "foo").GetAwaiter().GetResult();
        var resultBodyText = result.GetEnumerator();

        // Assert
        Assert.NotNull(resultBodyText);
    }
}



public class ConsumerPactClassFixture : IDisposable
{
    public IPactBuilder PactBuilder { get; private set; }
    public IMockProviderService MockProviderService { get; private set; }

    public int MockServerPort { get { return 9222; } }
    public string MockProviderServiceBaseUri { get { return String.Format("http://localhost:{0}", MockServerPort); } }

   public ConsumerPactClassFixture()
   {
       var pactConfig = new PactConfig
       {
           SpecificationVersion = "2.0.0",
           PactDir = @"..\..\..\..\..\pacts",
           LogDir = @".\pact_logs"
       };

       PactBuilder = new PactBuilder(pactConfig);

       PactBuilder.ServiceConsumer("Consumer")
           .HasPactWith("Provider");

       MockProviderService = PactBuilder.MockService(MockServerPort);
    }

    #region IDisposable Support
    private bool disposedValue = false; // To detect redundant calls

    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                // This will save the pact file once finished.
                PactBuilder.Build();
            }

            disposedValue = true;
        }
    }

    // This code added to correctly implement the disposable pattern.
    public void Dispose()
    {
        // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
        Dispose(true);
    }
    #endregion
}

当我运行测试时,我收到此错误:

dotnet test --filter "FullyQualifiedName=Bond.Publisher.Tests.ContractTest.ConsumerPactTests.ItHandlesInvalidDateParam"

测试运行 c:\Workspace\prod\test\Bond.Publisher.Tests\bin\Debug\netcoreapp3.1\Bond.Publisher.Tests.dll(.NETCoreApp,Version=v3.1) Microsoft (R) 测试执行命令Line Tool 版本 16.7.0 版权所有 (c) Microsoft Corporation。版权所有。

开始测试执行,请稍候...

共有 1 个测试文件与指定的模式匹配。[xUnit.net 00:00:10.95] Bond.Publisher.Tests.ContractTest.ConsumerPactTests.ItHandlesInvalidDateParam [失败] X Bond.Publisher.Tests.ContractTest.ConsumerPactTests.ItHandlesInvalidDateParam [4s 196ms] 错误消息:System.Net.Http.HttpRequestException : 无法连接,因为目标机器主动拒绝。---- System.Net.Sockets.SocketException : 由于目标机器主动拒绝,无法建立连接。堆栈跟踪:在 System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken) 在 System.Net.Http 的 System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancelToken)。 HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage 请求,

试运行失败。

我怀疑这可能是某种身份验证问题UnoAuthorisationHeaderMessageHandler.cs。我做错了什么?

4

2 回答 2

0

对我来说,这条路太长了。当我将项目移动到更靠近 C:\ 的文件夹时,测试运行了。

于 2021-02-16T16:08:43.153 回答
0

System.Net.Sockets.SocketException :无法建立连接,因为目标机器主动拒绝了它。通常在没有服务器监听您发送的 url 时发生。

检查 ruby​​ 服务是否启动并运行(测试运行程序启动它),您应该在Visual Studio下的任务管理器中看到它

或者,在您调用pactBuilder.Build()之前,您应该能够通过 PostMan 向 http://localhost:9222/instruments 发出 HTTP 请求...

于 2021-02-17T11:40:07.050 回答