2017-08-30 28 views
1

我試圖讓我的腦袋約PACT,我正在使用PACT-Net庫來實現此目的。C#PACT - 消費者驅動的聯繫測試 - 爲提供者編寫單元測試

我對消費者的單元測試工作正常,但我試圖在提供程序上設置單元測試。我使用的是在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

所以說話的同事,得到一個提示後,則下跌到啓動類。我沒有意識到它有效地啓動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); 
    } 
} 

和我的單元測試通過。希望這可以幫助某人。