【问题标题】:Integration test Asp.Net Core attribute parameter modification集成测试 Asp.Net Core 属性参数修改
【发布时间】:2018-11-21 16:54:06
【问题描述】:

我有一个端点方法,它有一个自定义属性,例如:

[Route ("/version")]
[CacheFilter (hours: 12)]
public async Task<ActionResult<string>> Version ()
{ ... }

我的缓存过滤器在其构造函数中设置了指定的缓存时间:

public CacheFilterAttribute (int days = 0, int hours = 0, int minutes = 0, int seconds = 0) : base (typeof (CacheFilter))
{
    this.days = days;
    this.hours = hours;
    this.minutes ...
}

我想知道当我测试该端点时是否有可能将该时间更改为(例如)10 秒。

我在单元测试中这样调用端点方法:

await RADBServer.Client.GetAsync ("/version");

提前非常感谢

【问题讨论】:

  • 单元测试?还是集成测试?该属性是仅在运行时适用于实际管道的元数据。没有办法改变它,因为它与编译时间细节有关。
  • 是的,集成测试,抱歉。我刚刚编辑过

标签: c# asp.net attributes integration-testing endpoint


【解决方案1】:

这是可以做到的。对于集成测试,使用 testserver 只需将全局过滤器添加到您的 MVC,您需要实现 IActionFilter。然后在 .OnActionExecuting 方法上,您可以访问特定的方法属性以更改其公共属性(如在属性构造函数上传递的值)。您的方法过滤器将在具有新属性值的全局过滤器之后调用。

ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.Filters.Add(typefof(TestFilter))...

{ public class TestFilter : IActionFilter { public void OnActionExecuting(ActionExecutingContext context) { // obtain the method attributes and change values } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    相关资源
    最近更新 更多