1

我正在努力了解PACT,我正在使用PACT-Net库来实现这一点。

我对消费者的测试工作正常,但我正在尝试在提供者上设置测试。我正在使用在 Visual Studio 中使用 Web API 模板时加载的基本 Web API 项目——它创建了 Values API 控制器。我只是在测试 GetIEumerable<string>方法作为流程的端到端测试。我也在关注 PACT-Net github 站点上的示例。这是我到目前为止的测试:

    [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();
    }
}

每当我运行单元测试时,都会出现以下错误:

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

我想我的问题是,我是否需要实际测试对 /api/values 的 HTTP 调用,还是我遗漏了其他东西?

谢谢

4

1 回答 1

2

因此,在与同事交谈并得到提示后,就到了 Startup 课程。我没有意识到它有效地启动了 Web 应用程序,所以

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

调用应用程序中的启动类:

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);
    }
}

我的单元测试通过了。希望这可以帮助某人。

于 2017-08-30T13:57:36.730 回答