I have a http post method for whose testing I have written the test below :
public async Task TestAdd_NewLicenseType()
{
using(var context = new LicenseGenContext(ContextOptions))
{
var controller = new LicenseTypeController(context);
var newLiceneType = new LicenseType()
{
LicenseTypeId = Guid.NewGuid(),
LicenseTypeName = "TypeOne",
LicenseTypeDescription = "License for StandAlone players"
};
var result = await controller.AddAsync(newLiceneType);
Assert.IsAssignableFrom<CreatedAtActionResult>(result);
}
}
The Http post is written in the controller as below :
[HttpPost]
[Route("AddLicenseType")]
public async Task<ActionResult> AddAsync(LicenseType newLicenseType)
{
if (!ModelState.IsValid)
{
return new Microsoft.AspNetCore.Mvc.BadRequestObjectResult(ModelState);
}
var result = await _context.LicenseTypes.AddAsync(newLicenseType);
_context.SaveChanges();
return CreatedAtAction("FindById", new { id = newLicenseType.LicenseTypeId }, newLicenseType);
}
I have created an HttpGet method and named it FindById which is referred to in the post method :
[HttpGet("{id}", Name ="FindById")]
//[ActionName("FindById")]
private async Task<ActionResult<LicenseType>> FindById(Guid id )
{
var licenseType = await _context.LicenseTypes.FirstOrDefaultAsync(e => e.LicenseTypeId == id);
if (licenseType == null)
{
return NotFound();
}
else
{
return licenseType;
}
}
The test passes and when I call this API with the right parameters, I can see a new entry in the database. However the Swagger UI testing tool gives me the below error
System.InvalidOperationException: No route matches the supplied values.
at Microsoft.AspNetCore.Mvc.CreatedAtActionResult.OnFormatting(ActionContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsyncCore(ActionContext context, ObjectResult result, Type objectType, Object value)
at Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsync(ActionContext context, ObjectResult result)
at Microsoft.AspNetCore.Mvc.ObjectResult.ExecuteResultAsync(ActionContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultAsync(IActionResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
I have read a few of the similar issues on StackOverflow but none of the solutions (removing Async from end of the method name, adding SuppressAsyncSuffixInActionNames option in my startup file )work for me.
Can someone please tell me if there is a problem with the current implementation?