小编Kas*_*r P的帖子

如何在ASP.NET Core 1 MVC 6中模拟IFormFile进行单元/集成测试?

我想编写用于在ASP.NET Core 1中上传文件的测试,但似乎找不到一种模拟/实例化从IFormFile派生的对象的好方法.有关如何做到这一点的任何建议?

谢谢.

c# asp.net unit-testing asp.net-core-mvc

19
推荐指数
3
解决办法
9718
查看次数

在ASP.NET Core Web Api中具有多个具有多个查询字符串参数的get方法

我正在构建一个web api,我有一个资源,必须有3个get方法,如下所示:

    [HttpGet]
    [Route("{city}/{streetName}/{streetNumber}/{littera}")]
    public IActionResult GetByAddress([FromQuery]string city, [FromQuery]string streetName, [FromQuery]int streetNumber, [FromQuery]string littera)
    {
        var model = _availabilityService.FindByAddress(city, streetName, streetNumber, littera);
        return Ok(model);
    }

    [HttpGet("{pointId}")]
    public IActionResult GetByPointId(string pointId)
    {
        var model = _availabilityService.FindByPointId(pointId);
        return Ok(model);
    }

    [HttpGet]
    [Route("{xCoordinate}/{yCoordinate}")]
    public IActionResult GetByCoordinates([FromQuery]decimal xCoordinate, [FromQuery]decimal yCoordinate)
    {
        var model = _availabilityService.FindByCoordinates(xCoordinate, yCoordinate);
        return Ok(model);
    }
Run Code Online (Sandbox Code Playgroud)

只有一个参数(pointId)的get方法工作正常,因为它不被视为查询字符串,而是id和id.然而,似乎剩下的两种方法无法通过ASP.NET中的路由器区分.

我在这里真的很茫然,无法弄清楚它为什么不起作用.我能够解决的是,如果我删除其中一种方法,另一种方法可以正常工作.

关于我做错的任何建议?

仅供参考,相应的网址:应该如下所示:

api/1.0/availabilities?city=Metropolis&streetName=Superstreet&streetNumber=1&littera=A
Run Code Online (Sandbox Code Playgroud)

/api/1.0/availabilities?xCoordinate=34.3444&yCoordinate=66.3422
Run Code Online (Sandbox Code Playgroud)

谢谢!

c# asp.net-core asp.net-core-routing

7
推荐指数
1
解决办法
1万
查看次数

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

我正在使用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();
    }
}
Run Code Online (Sandbox Code Playgroud)

PaymentService

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

 public class PaymentService : IPaymentService
{
    public string ReadPaymentFiles(string caller)
    {
        return caller;
    } …
Run Code Online (Sandbox Code Playgroud)

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

2
推荐指数
1
解决办法
932
查看次数