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

时间:2017-08-30 07:52:44

标签: c# pact pact-net

我正试图了解PACT并且我正在使用PACT-Net库实现这一目标。

我对消费者的测试工作正常,但我正在尝试在提供商上设置测试。我正在使用在Visual Studio中使用Web API模板时加载的基本Web API项目 - 它创建了Values API控制器。我只是测试Get IEumerable<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调用,还是我错过了其他内容?

由于

1 个答案:

答案 0 :(得分:2)

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

this.$bus.$on( ... )

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

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

我的单元测试通过了。希望这有助于某人。