我对消费者的测试运行良好,但我试图在提供者上设置测试。我使用的是在Visual中使用Web模板时加载的基本Web项目--它创建了值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/value的HTTP调用,还是遗漏了其他东西?
谢谢
发布于 2017-08-30 13:57:36
因此,在与同事交谈并得到提示后,这要归功于创业班。我没有意识到它有效地启动了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);
}
}我的单元测试通过了。希望这能帮上忙。
https://stackoverflow.com/questions/45954883
复制相似问题