mardi 29 mai 2018

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

I'm using SoapCore for creating a WCF-ish kind of application using asp.net core 2.

This works fine for me so for but I've somewhat hit a brickwall when it comes to integration testing my endpoints.

Since SoapCore is a middleware and has no relation to any api controllers I'm not able to use an HttpClient for testing the endpoints, hence the TestServer is of no use to me.

What I'm no wondering is how can I run kestrel and in the same time run my tests?

I don't think any code here might be of any use but what I got so far is as follows.

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

PaymentService

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

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

One of my tests:

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

The test does'nt do anything right now since it is not calling any live endpoint, which is my problem...

Aucun commentaire:

Enregistrer un commentaire