Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

Problem of discovering controllers in integration tests

Open hikalkan opened this issue 10 years ago • 24 comments

I'm using RC2.

I have a web project, say src\MyWeb, and a test project, say test\MyWeb.Tests. I read and followed the documentation (https://docs.asp.net/en/latest/testing/integration-testing.html).

When I put my controllers into Test project, if works. But if they are in the MyWeb project (which is normally should be) it can not find my controller and returns HttpStatusCode.NotFound.

Is that a known issue?

Additional info:

This is my test project: https://github.com/aspnetboilerplate/aspnet-core-template/blob/dev/test/AbpCompanyName.AbpProjectName.Web.Tests/Controllers/HomeController_Tests.cs And this is the actual working web project: https://github.com/aspnetboilerplate/aspnet-core-template/blob/dev/src/AbpCompanyName.AbpProjectName.Web/Controllers/HomeController.cs This will be an open source AspNet Core and EF Core based startup template. I'm just writing some test codes for now.

hikalkan avatar Jun 22 '16 15:06 hikalkan

/cc @pranavkm

rynowak avatar Jun 22 '16 15:06 rynowak

I am facing the same issue. My integration tests use TestServer to start the host with REST methods, obtain an HttpClient instance and make HTTP requests. The tests worked fine using RC1 but do not work using RC2. Actual actions are not called, status 404 is just returned for all URLs. Controllers are placed in a separate assembly, so maybe they are not detected correctly, but the same controllers are detected and work from a real web host.

barynov avatar Jun 23 '16 13:06 barynov

I am facing the same issues with my library - https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc and lost more than 4 hours debugging this.

The problem is with dotnet test - when using it, DependencyContext does not return all dependencies and MVC cannot find controllers from assemblies other than the current one (the web project is the other assembly here when running tests). It is interesting that with Visual Studio test runner (which calls dotnet test) dependency context is correct but from the CLI and the console - it is not. Console applications with dotnet run also returns correct dependencies, only the test command is failing for some reason. So in conclusion - MVC will find all candidate assemblies containing controllers and other application parts, because it runs dotnet run but when using dotnet test the dependencies will be different and the controllers will not be found.

Logged an issue last week, no one answered it yet - https://github.com/dotnet/cli/issues/3583

ivaylokenov avatar Jun 23 '16 13:06 ivaylokenov

@hikalkan, could you point me to the implementation of AbpAspNetCoreIntegratedTestBase? I'd like to see how you're setting up the TestServer.

pranavkm avatar Jun 23 '16 14:06 pranavkm

Sure, it's actually simple: https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.AspNetCore.TestBase/AbpAspNetCoreIntegratedTestBase.cs

hikalkan avatar Jun 23 '16 14:06 hikalkan

It might be a result of https://github.com/aspnet/Mvc/issues/4555. DependencyContext does not preserve transitive dependency information for secondary references and we worked around this in RTM.

Could you try pointing to our RTM bits (https://dotnet.myget.org/gallery/aspnet1) and see if your issue is addressed? These packages should become available on NuGet.org by next week.

pranavkm avatar Jun 23 '16 14:06 pranavkm

OK, I will try it, thanks.

hikalkan avatar Jun 23 '16 14:06 hikalkan

It will be better for me to wait packages on nuget. Because, I'm getting some unrelated exceptions after upgrading to myget packages (I just upgraded it from my test project and it produces assembly redirect exceptions, type not found exceptions and so on..) After it's fully released, I will be one of the first testers :)

hikalkan avatar Jun 23 '16 15:06 hikalkan

Fair enough. The feed that I pointed to is meant to reflect the closure of packages that will be uploaded to NuGet.org as part of RTM. If you see issues with it now, its likely you'll see them once you're on NuGet.org. If it's a problem with the packages, we'd love to know sooner.

pranavkm avatar Jun 23 '16 15:06 pranavkm

Hmm.. OK, I will upgrade also dependent projects and will inform you about the result.

hikalkan avatar Jun 23 '16 15:06 hikalkan

@hikalkan - a workaround that you could deploy immediately would like something like this (in your startup code).

public void ConfigureServices(IServiceContainer services)
{
    var mvc = services.AddMvc();
    var parts = mvc.PartManager.ApplicationParts;
    parts.Clear();
    parts.Add(new AssemblyPart(typeof(Startup).GetTypeInfo().Assembly));
}

This overrides the auto-discovery of controllers, and tells MVC to look for controllers only in the project containing your startup class.

rynowak avatar Jun 23 '16 15:06 rynowak

This is what I was searcing for.. thanks a lot :) Meanwhile, I'm trying to upgrade all my projects to see if there is a problem.

hikalkan avatar Jun 23 '16 15:06 hikalkan

The workaround works for me too, thanks!

barynov avatar Jun 23 '16 15:06 barynov

The workaround worked for me too, thanks, but there is another problem now: It can not find the View if action returns a view. This is probably because adding a reference to Web project (which contains views) from test project does not copy views to the project output. If I return simply Content, no problem. Do you know a solution for that?

hikalkan avatar Jun 23 '16 16:06 hikalkan

You need to specify the ContentRoot to point to your website's root directory. See https://github.com/aspnet/Mvc/blob/dev/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs#L38

pranavkm avatar Jun 23 '16 16:06 pranavkm

Oh, thank you very much for pointing that.

hikalkan avatar Jun 23 '16 16:06 hikalkan

Beside workarounds, I'm really interested in making RTM version working since I want to provide feedback about potential problems. Finally, I'm getting this error on appveyor (https://ci.appveyor.com/project/hikalkan/aspnetboilerplate/build/1.0.191):

Package Microsoft.DotNet.Cli.Utils 1.0.0-preview2-003121 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Microsoft.DotNet.Cli.Utils 1.0.0-preview2-003121 supports:
      - net451 (.NETFramework,Version=v4.5.1)
      - netstandard1.6 (.NETStandard,Version=v1.6)
    Package Microsoft.DotNet.ProjectModel.Loader 1.0.0-preview2-003121 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Microsoft.DotNet.ProjectModel.Loader 1.0.0-preview2-003121 supports: netstandard1.6 (.NETStandard,Version=v1.6)

    One or more packages are incompatible with .NETCoreApp,Version=v1.0.
    Package Microsoft.DotNet.ProjectModel 1.0.0-rc3-003121 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Microsoft.DotNet.ProjectModel 1.0.0-rc3-003121 supports:
      - net451 (.NETFramework,Version=v4.5.1)
      - netstandard1.6 (.NETStandard,Version=v1.6)
    One or more packages are incompatible with .NETCoreApp,Version=v1.0.
    Package Microsoft.DotNet.Cli.Utils 1.0.0-preview2-003121 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Microsoft.DotNet.Cli.Utils 1.0.0-preview2-003121 supports:
      - net451 (.NETFramework,Version=v4.5.1)
      - netstandard1.6 (.NETStandard,Version=v1.6)
    Package Microsoft.DotNet.ProjectModel 1.0.0-rc3-003121 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Microsoft.DotNet.ProjectModel 1.0.0-rc3-003121 supports:
      - net451 (.NETFramework,Version=v4.5.1)
      - netstandard1.6 (.NETStandard,Version=v1.6)
    One or more packages are incompatible with .NETCoreApp,Version=v1.0.
    Package Microsoft.VisualStudio.Web.CodeGeneration 1.0.0-preview2-final is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Microsoft.VisualStudio.Web.CodeGeneration 1.0.0-preview2-final supports:
      - net451 (.NETFramework,Version=v4.5.1)
      - netstandard1.6 (.NETStandard,Version=v1.6)
    One or more packages are incompatible with .NETCoreApp,Version=v1.0.

My project is .NET 4.6.1 and it's project.json is here: https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev-aspnetcore1/test/aspnet-core-demo/AbpAspNetCoreDemo/project.json

Is that related to dotnet cli version?

hikalkan avatar Jun 23 '16 19:06 hikalkan

You have to update to the RTM CLI. Grab the latest for your platform from here - https://github.com/dotnet/cli#installers-and-binaries.

pranavkm avatar Jun 23 '16 19:06 pranavkm

Thank you. I'm downloading .NET Core SDK Installer.

But, notice that ".NET Core Installer" link is somehow broken: https://dotnetcli.blob.core.windows.net/dotnet/Sdk/rel-1.0.0/dotnet-win-x64.latest.exe

When I enter this URL, result is:

<Error>
<Code>BlobNotFound</Code>
<Message>
The specified blob does not exist. RequestId:c03a2313-0001-0130-4a87-cd7afa000000 Time:2016-06-23T19:41:11.5468688Z
</Message>
</Error>

hikalkan avatar Jun 23 '16 19:06 hikalkan

Try https://dotnetcli.blob.core.windows.net/dotnet/preview/Installers/1.0.0-preview2-003121/dotnet-dev-win-x64.1.0.0-preview2-003121.exe. The rel-1.0.0 channel is for post RTM.

pranavkm avatar Jun 23 '16 20:06 pranavkm