简体   繁体   中英

Integration test Asp.Net Core attribute parameter modification

I've got an endpoint method that has a custom attribute like:

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

And my cache filter sets that specified cache time in its constructor:

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 ...
}

I would like to know if when I'm testing that endpoint is any possibility to change that time to (for example) 10 seconds.

I call that endpoint method in my unit test this way:

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

Thanks a lot in advance

It can be done. For integatrion test, using the testserver just add a global filter to your MVC, your need to implement the IActionFilter. Then on the.OnActionExecuting method you can access the specific method attribute to change its public properties (like the values passed on the attribute constructor). Your method filter will be called after the global filter with the new properties values.

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 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM