简体   繁体   English

C#PACT-消费者驱动的联系人测试-提供商的写作测试

[英]C# PACT - Consumer Driven Contact testing - writing test for provider

I am trying to get my head about PACT and I am using the PACT-Net library to achieve this. 我正在努力使自己对PACT有所了解,并且正在使用PACT-Net库来实现这一目标。

My tests on the Consumer are working fine but I am trying to setup the tests on the Provider. 我对使用者的测试工作正常,但是我正在尝试对提供者进行测试。 I am using the basic Web API project that loads when you use the Web API template within Visual Studio - which creates the Values API controller. 我正在使用在Visual Studio中使用Web API模板时加载的基本Web API项目,该模板创建了Values API控制器。 I am just testing the Get IEumerable<string> method as an end to end test of the process. 我只是在测试Get IEumerable<string>方法作为该过程的端到端测试。 I am also following the example on the PACT-Net github site. 我也在PACT-Net github站点上关注该示例。 Here is the test I have so far: 这是我到目前为止的测试:

    [Fact]
    public void EnsureValuesReturnedFromApi()
    {
        var config = new PactVerifierConfig
        {
            Outputters = new List<IOutput>
            {
                new XUnitOutput(_output)
            }
        };

    using (WebApp.Start<TestStartup>(serviceUri))
    {
        var pactVerifier = new PactVerifier(config);
        pactVerifier.ProviderState($"{serviceUri}/provider-states")
            .ServiceProvider("Values API", serviceUri)
            .HonoursPactWith("Consumer")
            .PactUri("http://localhost:8080/pacts/provider/Values%20API/consumer/Consumer/latest")
            .Verify();
    }
}

When ever I run the unit test I get the following error: 每当我运行单元测试时,都会出现以下错误:

Reading pact at http://localhost:8080/pacts/provider/Values%20API/consumer/Consumer/latest

Verifying a pact between Consumer and Values API
  Given When I want the values
    Getting a list
      with GET /api/values
        returns a response which
          has status code 200 (FAILED - 1)
          has a matching body (FAILED - 2)
          includes headers
            "Accept" with value "application/json" (FAILED - 3)
            "Content-Type" with value "application/json" (FAILED - 4)

Failures:

  1) Verifying a pact between Consumer and Values API Given When I want the values Getting a list with GET /api/values returns a response which has status code 200
     Failure/Error: set_up_provider_state interaction.provider_state, options[:consumer]

     Pact::ProviderVerifier::SetUpProviderStateError:
       Error setting up provider state 'When I want the values' for consumer 'Consumer' at http://localhost:9222/provider-states. response status=500 response body=

  2) Verifying a pact between Consumer and Values API Given When I want the values Getting a list with GET /api/values returns a response which has a matching body
     Failure/Error: set_up_provider_state interaction.provider_state, options[:consumer]

     Pact::ProviderVerifier::SetUpProviderStateError:
       Error setting up provider state 'When I want the values' for consumer 'Consumer' at http://localhost:9222/provider-states. response status=500 response body=

  3) Verifying a pact between Consumer and Values API Given When I want the values Getting a list with GET /api/values returns a response which includes headers "Accept" with value "application/json"
     Failure/Error: set_up_provider_state interaction.provider_state, options[:consumer]

     Pact::ProviderVerifier::SetUpProviderStateError:
       Error setting up provider state 'When I want the values' for consumer 'Consumer' at http://localhost:9222/provider-states. response status=500 response body=

  4) Verifying a pact between Consumer and Values API Given When I want the values Getting a list with GET /api/values returns a response which includes headers "Content-Type" with value "application/json"
     Failure/Error: set_up_provider_state interaction.provider_state, options[:consumer]

     Pact::ProviderVerifier::SetUpProviderStateError:
       Error setting up provider state 'When I want the values' for consumer 'Consumer' at http://localhost:9222/provider-states. response status=500 response body=

1 interaction, 1 failure

Failed interactions:

* Getting a list given When I want the values

I guess my question is, do I need to actually test the HTTP calls to /api/values or am I missing something else? 我想我的问题是,我是否需要实际测试对/ api / values的HTTP调用,还是缺少其他内容?

Thanks 谢谢

So after speaking to a colleague and getting a hint, it was down to the Startup class. 因此,在与同事交谈并获得提示后,该问题由Startup类负责。 I hadn't realised it effectively starts the web application so 我没有意识到它可以有效地启动Web应用程序,所以

public class TestStartup
{
    public void Configuration(IAppBuilder app)
    {
        var startup = new Startup();
        app.Use<ProviderStateMiddleware>(); 
        startup.Configuration(app);
    }
}

calls the startup class within the application: 在应用程序内调用启动类:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var httpConfig = new HttpConfiguration();
        httpConfig.MapHttpAttributeRoutes();

        httpConfig.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        var appXmlType = httpConfig.Formatters.XmlFormatter.SupportedMediaTypes
                                   .FirstOrDefault(t => t.MediaType == "application/xml");
        httpConfig.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

        app.UseWebApi(httpConfig);
    }
}

and my unit test passed. 我的单元测试通过了。 Hope this helps someone. 希望这对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM