Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

TestServer + MVC + Razor = The type or namespace could not be found. #6908

Closed
dpedrelli opened this issue Oct 4, 2017 · 3 comments
Closed
Assignees

Comments

@dpedrelli
Copy link

I am using VS 2017 15.3.5. I have an MVC project that was created with the .NET Core 2.0 template and targets netcoreapp2.0. I have a test project that was created with the xUnit Test Project (.NET Core) template and targets netcoreapp2.0. I am running on Windows. When I hit the AccountController.Login() GET method and return the Login.cshtml view, I get the same error as described in #1731.

The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
Predefined type 'System.Object' is not defined or imported
The type or namespace name 'ViewFeatures' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)

The work around described in #4818 resolves the issue, but is impractical for running on other machines or deploying to a server.

Additionally, I have a wrapper class, for an HttpClient. It works supports Identity, IdentityServer, and Antiforgery. If I run the MVC app in a separate instance of VS, without using TestServer, the test run, and succeed. If I set my wrappers HttpClient, with TestServer.CreateClient(), the AccountControler.Login() POST method does not execute because of the Antiforgery token. Does HttpServer not support Antiforgery?

@Eilon
Copy link
Contributor

Eilon commented Oct 4, 2017

@javiercn can you take a look at this? Do the new test helpers you've built help in this scenario?

@javiercn
Copy link
Member

javiercn commented Oct 4, 2017

There are a few things that you need to have in order to run an MVC application in memory.

  • You need to copy the .deps file of your project into the test bin folder (look at the targets file below)
  • You need to set the content root to point to the project folder (with the extension method below)

We will be releasing this as a package in the next release, but for now you can just put it in your csproj and in your test project.

See
https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Testing/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Testing.targets

https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationTestFixture.cs#L93

https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNetCore.TestHost/WebHostBuilderExtensions.cs#L52

@javiercn javiercn closed this as completed Oct 4, 2017
@dpedrelli
Copy link
Author

@javiercn if I follow you, I need to copy my MVC's .deps.json to the bin directory of my test project. I did that. Then, I need to add WebHostBuilderExtensions and WebApplicationTestFixture to my test project, so that I can use .UseSolutionRelativeContentRoot(). I don't get any errors with the using clauses, but both classes have errors in them.

WebApplicationTestFixture

throw new InvalidOperationException(Resources.FormatMissingDepsFile(
                    depsFile.FullName,
                    Path.GetFileName(depsFile.FullName)));

"Resources is inaccessible due to its protection level."

        protected virtual IWebHostBuilder CreateWebHostBuilder() =>
            WebHostBuilderFactory.CreateFromTypesAssemblyEntryPoint<TStartup>(Array.Empty<string>()) ?? new WebHostBuilder();

"The name WebHostBuilderFactory does not exist in the current context.

I ensured that the test project has both Microsoft.AspNetCore.Hosting and Microsoft.Extensions.DependencyInjection installed.

WebHostBuilderExtensions has four lines with errors.

            webHostBuilder.ConfigureServices(
                s => s.AddSingleton<IStartupConfigureServicesFilter>(
                    new ConfigureTestServicesStartupConfigureServicesFilter(servicesConfiguration)));

"The type of namespace IStartupConfigureServicesFilter could not be found. (are you missing a using directive or an assembly reference?)

            webHostBuilder.ConfigureServices(
                s => s.AddSingleton<IStartupConfigureContainerFilter<TContainer>>(
                    new ConfigureTestServicesStartupConfigureContainerFilter<TContainer>(servicesConfiguration)));

"The type of namespace IStartupConfigureContainerFilter could not be found. (are you missing a using directive or an assembly reference?)

private class ConfigureTestServicesStartupConfigureServicesFilter : IStartupConfigureServicesFilter
"The type of namespace IStartupConfigureServicesFilter could not be found. (are you missing a using directive or an assembly reference?)

private class ConfigureTestServicesStartupConfigureContainerFilter<TContainer> : IStartupConfigureContainerFilter<TContainer>
"The type of namespace IStartupConfigureContainerFilter could not be found. (are you missing a using directive or an assembly reference?)

If I setup TestServer with

            var dir = Directory.GetCurrentDirectory();
            dir = dir.Replace(@"SM.Core.X.Tests\bin\Debug\netcoreapp2.0", @"SM.Mvc");
            _webHostBuilder =
                WebHost.CreateDefaultBuilder()
                    .UseStartup<Startup>()
                    .UseContentRoot(dir);

I get the error: System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.VisualStudio.Web.CodeGeneration, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'

If I install Microsoft.VisualStudio.Web.CodeGeneration into the test project, I get the error:
System.IO.FileNotFoundException: 'Could not load file or assembly 'dotnet-aspnet-codegenerator-design, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants