Reputation: 5850
For some reason, when ASP.NET core controllers are created in separate assembly, the TestServer is not able to find controller actions when the client makes a request.(results in 404 response) Why is this? How can I work around it? Here are steps to reproduce.
See the follwing link for creating the integration tests. Integration testing w/ ASP.NET Core
Upvotes: 42
Views: 14112
Reputation: 1756
For those of you experiencing this during a migration to netcoreapp3.0
, I found the above answers work, but you can do this more cleanly by changing the reference in your .csproj file itself perhaps.
In my case I changed the first XML line
<Project Sdk="Microsoft.NET.Sdk">
to
<Project Sdk="Microsoft.NET.Sdk.Web">
Credits to: https://github.com/aspnet/Mvc/issues/5992#issuecomment-395408983
Upvotes: 36
Reputation: 176
If you follow the pre-requisites from the MS documentation the controllers from separate assemblies are loaded.
In my case adding Microsoft.AspNetCore.Mvc.Testing NuGet package fixed the issue and I don't need to call AddApplicationPart
anymore.
Upvotes: 15
Reputation: 5850
Actually I found a solution for now, see diff below:
It sounds like this may be bug of TestServer() class and how it is hosting the application during the test run.
Here is the line of code in case you cannot read above in image
.AddApplicationPart(Assembly.Load(new AssemblyName("WebApiToReproduceBug.Controllers")));
Upvotes: 52
Reputation: 5487
In addition to joey answer:
There is no need to call Assembly.Load() to resolve this bug. You can use the code below. ServiceHookController
is class from a separate project.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddApplicationPart(typeof(ServiceHookController).Assembly);
}
Upvotes: 22