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

标签 c# testing asp.net-core asp.net-core-2.0 kestrel-http-server

我正在使用 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/

相关文章:

c# - Html Agility Pack - 如何选择正确的跨度类

c# - 通过 OracleDataReader 读取时处理 NULL 值?

ruby-on-rails - 如何使用 RSpec 更新测试模型中的属性

c# - ASP Core 违反了 String 类型参数 'TRole' 的约束

c# - 我们怎样才能返回一个与方法返回类型不同的对象呢?

asp.net - 如何在像DDD这样的ASP.NET CORE解决方案中组织多个项目?

c# - 关于从 C# 使用 COM 的任何好的教程?

c# - 对 DataGridView 中的选定行进行排序

c# - Ranorex 驱动的自动化测试数据,验证返回 False 时的下一个案例。

javascript - Jest 中的嵌套模拟函数