簡體   English   中英

在不使用Microsoft.AspNetCore.TestHost中包含的TestServer的情況下運行Kestrel進行測試

[英]Running Kestrel for testing without using the TestServer included in Microsoft.AspNetCore.TestHost

我正在使用SoapCore使用asp.net core 2創建WCF類的應用程序。

這樣做對我來說很好,但是在集成測試端點時遇到了一些障礙。

由於SoapCore是中間件,並且與任何api控制器都沒有關系,所以我無法使用HttpClient來測試端點,因此TestServer對我沒有用。

我的問題是在不使用TestServer的情況下如何與我的集成測試並行運行kestrel,或者在這種情況下是否可以利用TestServer?

我認為這里的任何代碼都沒有用,但是到目前為止,我得到的是以下內容。

啟動文件

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();
    }
}

PaymentService

[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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM