.NET6 中的依赖注入(用于使用 PactNet 进行合约测试)

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

我尝试初始化我的测试类,其中包括服务和客户端。但是,我继续遇到以下错误:xUnit1041:Fixture 参数“HttpClient”没有 Fixture 源。如果它源自集合定义,请确保定义位于与测试相同的程序集中。

我很困惑为什么构造函数中的两个参数都会出现此错误。

createOrder方法,只是创建一个DTO。服务方法等待 OrderDTO

public class ContractTestExample
    {
        private readonly IPactBuilderV4 pact;
        private readonly Service _service;
        private readonly HttpClient _httpClient;

        public ContractTestExample(HttpClient _httpClient, Service _service)
        {

            _httpClient = HttpClient;
            _service = Service;
            pact = Pact.V4("Consumer", "Provider", new PactConfig()).WithHttpInteractions();
        }

        [Fact]
        public async Task GetSomething()
        {
                pact
                .UponReceiving("A GET request to retrieve transaction")
                .Given("Transaction with id 1234")
                .WithRequest(HttpMethod.Get, "getSmth/1234")
                .WillRespond()
                .WithStatus(HttpStatusCode.OK)
                .WithHeader("Content-Type", "application/json; charset=utf-8")
                .WithJsonBody(new { id = "1234" });

               await pact.VerifyAsync( async ctx =>
                {
                    _httpClient.BaseUri = ctx.MockServerUri;

                    var response =  await _service.transferToService(createOrder());

                    Assert.NotEmpty(response.Status);

                });
        }

    }

服务构造函数等待 5 个参数(客户端是其中之一)

我尝试使用 CollectionServices,但对象给了我一个 NullPointer。

c# testing .net-6.0 xunit pact-net
1个回答
0
投票
// <auto-generated>
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>

namespace HeimdallSwagger
{
    using Microsoft.Rest;
    using Microsoft.Rest.Serialization;
    using Newtonsoft.Json;
    using System.Collections;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Http;

    public partial class ClientAPI : ServiceClient<ClientAPI>, IClientAPI
    {

        public System.Uri BaseUri { get; set; }

        public JsonSerializerSettings SerializationSettings { get; private set; }

        public JsonSerializerSettings DeserializationSettings { get; private set; }

        public ServiceClientCredentials Credentials { get; private set; }


        protected ClientAPI(HttpClient httpClient, bool disposeHttpClient) : base(httpClient, disposeHttpClient)
        {
            Initialize();
        }

        private void Initialize()
        {
            OrderPush = new OrderPush(this);
            SerializationSettings = new JsonSerializerSettings
            {
                Formatting = Newtonsoft.Json.Formatting.Indented,
                DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
                DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc,
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize,
                ContractResolver = new ReadOnlyJsonContractResolver(),
                Converters = new  List<JsonConverter>
                    {
                        new Iso8601TimeSpanConverter()
                    }
            };
            DeserializationSettings = new JsonSerializerSettings
            {
                DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
                DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc,
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize,
                ContractResolver = new ReadOnlyJsonContractResolver(),
                Converters = new List<JsonConverter>
                    {
                        new Iso8601TimeSpanConverter()
                    }
            };
            CustomInitialize();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.