gpt4 book ai didi

c# - 在不使用 Microsoft.AspNetCore.TestHost 中包含的 TestServer 的情况下运行 Kestrel 进行测试

转载 作者:行者123 更新时间:2023-11-28 20:03:40 25 4
gpt4 key购买 nike

我正在使用 SoapCore 使用 asp.net core 2 创建类似 WCF 的应用程序。

这对我来说很好用,但在集成测试我的端点时,我有点碰壁了。

由于 SoapCore 是一个中间件并且与任何 api Controller 无关,我无法使用 HttpClient 来测试端点,因此 TestServer 对我没有用。

我的问题是如何在不使用 TestServer 的情况下同时运行 kestrel 和我的集成测试,或者在这种情况下有没有办法利用 TestServer?

我不认为这里的任何代码可能有任何用处,但到目前为止我得到的如下。

启动.cs

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IPaymentService>(service => new Services.PaymentService());
services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseSoapEndpoint<IPaymentService>("/PaymentService.svc", new BasicHttpBinding());
app.UseMvc();
}
}

支付服务

[ServiceContract]
public interface IPaymentService
{
[OperationContract]
string ReadPaymentFiles(string caller);
}

public class PaymentService : IPaymentService
{
public string ReadPaymentFiles(string caller)
{
return caller;
}
}

我的一个测试:

public void Should_Get_Soap_Response_From_PaymentService()
{
var testServerFixture = new TestServerFixture();
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress(new Uri("http://localhost:5000/PaymentService.svc"));
var channelFactory = new ChannelFactory<IPaymentService>(binding, endpoint);

var serviceClient = channelFactory.CreateChannel();
var response = serviceClient.ReadPaymentFiles("Ping");
channelFactory.Close();
}

测试现在没有做任何事情,因为它没有调用任何实时端点,这是我的问题...

最佳答案

您可以使用像 Microsoft.AspNetCore.Hosting 包这样的自托管。在执行测试之前,您可以运行您的 webHost,然后在此主机上执行您的文本。

public MyTestStartup()
{
_webhost = WebHost.CreateDefaultBuilder(null)
.UseStartup<Startup>()
.UseKestrel()
.UseUrls(BASE_URL)
.Build();
_webhost.Start();
}

关于c# - 在不使用 Microsoft.AspNetCore.TestHost 中包含的 TestServer 的情况下运行 Kestrel 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50578907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com