我把WireMock.Net实现在ASP.NET Core WebApi的program.cs中,applicationUrl为 https:/localhost:5001。 并在其中有一个未使用的控制器。
我想模拟发送到https:localhost:5001home的请求来响应bodyContent对象。但是只有在端口不同的情况下才有效。但是在这种情况下,模拟将无法工作,因为它将作为微服务放在云架构中。
_server = WireMockServer.Start(
new WireMockServerSettings
{
Urls = new[] { "https://localhost:5001"},
ReadStaticMappings = true
});
_server.Given(Request.Create()
.WithPath("/home"))
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "aplication/json")
.WithBodyAsJson(bodyContent));
在你的代码示例中使用的WireMock.Net将监听到 https:/localhost:5001。.但是为了让他的工作,你只需要在Program.cs中使用这段代码,或者使用单机版.请看。https:/github.comWireMock-NetWireMock.NetwikiWireMock-as-a-standalone-process。
一旦设置完毕,你的WebApi项目就可以在以下地址调用WireMock.Net了 https:/localhost:5001。 而不是连接到真正的外部服务。
我希望这一点是清楚的,否则你需要更多的解释你要实现的是什么。