首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#协议.消费者驱动的接触测试.提供者的书写测试

C#协议.消费者驱动的接触测试.提供者的书写测试
EN

Stack Overflow用户
提问于 2017-08-30 07:52:43
回答 1查看 3.5K关注 0票数 1

我正试图了解协约,并且正在使用PACT网库来实现这一点。

我对消费者的测试运行良好,但我试图在提供者上设置测试。我使用的是在Visual中使用Web模板时加载的基本Web项目--它创建了值API控制器。我只是测试Get IEumerable<string>方法作为对流程的端到端测试。我也在仿效PACT-Net github网站上的例子。下面是我到目前为止的测试:

代码语言:javascript
复制
    [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();
    }
}

当我运行单元测试时,我会得到以下错误:

代码语言:javascript
复制
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/value的HTTP调用,还是遗漏了其他东西?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-30 13:57:36

因此,在与同事交谈并得到提示后,这要归功于创业班。我没有意识到它有效地启动了web应用程序,所以

代码语言:javascript
复制
public class TestStartup
{
    public void Configuration(IAppBuilder app)
    {
        var startup = new Startup();
        app.Use<ProviderStateMiddleware>(); 
        startup.Configuration(app);
    }
}

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

代码语言:javascript
复制
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);
    }
}

我的单元测试通过了。希望这能帮上忙。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45954883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档