This repository has been archived by the owner. It is now read-only.

Integration tests with ASP.NET Core causes missing references from Razor files #1212

Closed
BrianVallelunga opened this Issue Apr 12, 2017 · 27 comments

Comments

Projects
None yet
@BrianVallelunga
Copy link

BrianVallelunga commented Apr 12, 2017

Title

Integration tests with ASP.NET Core causes missing references from Razor files

Functional impact

When attempting to run an integration test against an ASP.NET Core 1.1.1 site using the full .NET Framework, Razor views are unable to resolve references to namespaces like System

Minimal repro steps

  1. Build the attached
    IntegrationTestSample.zip
    solution.
  2. Run the site and confirm the home index page works
  3. Use the Test Explorer to run the GetHomeReturnsOKAsync test

Expected result

The test should pass with a successful OK response from the server.

Actual result

The test fails with errors such as:

[xUnit.net 00:00:06.8955637] XUnitTestProject1.Tests.GetHomeReturnsOKAsync [FAIL]
[xUnit.net 00:00:06.8973988] Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException : One or more compilation failures occurred:
[xUnit.net 00:00:06.8978938] 0q2sc41d.qfp(10,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
[xUnit.net 00:00:06.8984156] 0q2sc41d.qfp(11,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
[xUnit.net 00:00:06.8988809] 0q2sc41d.qfp(12,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
[xUnit.net 00:00:06.8993828] 0q2sc41d.qfp(15,36): error CS0234: The type or namespace name 'ViewFeatures' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
[xUnit.net 00:00:06.8998931] 0q2sc41d.qfp(16,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
[xUnit.net 00:00:06.9003332] 0q2sc41d.qfp(18,86): error CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?
[xUnit.net 00:00:06.9007670] 0q2sc41d.qfp(18,86): error CS0518: Predefined type 'System.Boolean' is not defined or imported

Further technical details

This seems to be a regression of #755 and aspnet/Mvc#4686

Unlike those other issues, there is no third project involved here. Just the site and test projects.

This was not an issue until we switched to VS 2017 from VS 2015 (with project.json and xproj).

@rynowak

This comment has been minimized.

Copy link
Member

rynowak commented Apr 12, 2017

/cc @pranavkm

@BrianVallelunga

This comment has been minimized.

Copy link

BrianVallelunga commented Apr 24, 2017

Can anyone reproduce this with the sample I uploaded?

@aruss

This comment has been minimized.

Copy link

aruss commented Apr 24, 2017

Same problem here. With the previous Version it worked as I upgraded to the latest one I can not run integration tests anymore. Also tried your code and it does not work.

@hhalim

This comment has been minimized.

Copy link

hhalim commented Apr 24, 2017

@BrianVallelunga I can reproduce using the sample you provided. I also experienced the issue with @using in View not working with referencing dll directly. The dll was created in .net 4.6.1 and my web app is .net core 1.1.1 running in full .net framework in VS2017. I believe this is similar to issue 4686.

@BrianVallelunga

This comment has been minimized.

Copy link

BrianVallelunga commented Apr 24, 2017

Thanks for the confirmation. Hopefully we can get a workaround soon. This is holding up our move to VS 2017 right now.

@NTaylorMullen

This comment has been minimized.

Copy link
Member

NTaylorMullen commented Apr 24, 2017

This looks like similar issues we encountered when migrating MVC. Give this a try.

@BrianVallelunga

This comment has been minimized.

Copy link

BrianVallelunga commented Apr 24, 2017

@NTaylorMullen I tried your suggestion and even manually copied the .deps.json file over to the test project. Unfortunately this did not fix the issue, though I agree that it seems like it could be related.

@BrianVallelunga

This comment has been minimized.

Copy link

BrianVallelunga commented Apr 27, 2017

Can someone from Microsoft please comment on this?

@pranavkm pranavkm self-assigned this Apr 27, 2017

@rynowak

This comment has been minimized.

Copy link
Member

rynowak commented Apr 27, 2017

@pranavkm @NTaylorMullen - any more suggestions here?

@rynowak

This comment has been minimized.

Copy link
Member

rynowak commented Apr 28, 2017

@BrianVallelunga - Hi, I tried your project and was able to reproduce the failure, as well as fix it.

These are a few known issues that we've already worked around in our (MVC team) functional tests. We need to do a better job getting this information out there. I've also started a conversation internally about how we can make this much much easier to figure out for simple MVC apps.

Here's the complete project file with the fixes in place:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>    
    <TargetFramework>net461</TargetFramework>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.1.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
    <PackageReference Include="xunit" Version="2.2.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\IntegrationTestSite\IntegrationTestSite.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
  </ItemGroup>
  
  <ItemGroup>
    <None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

  <!--
    Work around https://github.com/NuGet/Home/issues/4412. MVC uses DependencyContext.Load() which looks next to a .dll
    for a .deps.json. Information isn't available elsewhere. Need the .deps.json file for all web site applications.
  -->
  <Target Name="CopyDepsFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
    <ItemGroup>
      <DepsFilePaths Include="$([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json'))" />
    </ItemGroup>

    <Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
  </Target>
</Project>

You also need to create an xunit.runner.json - that is just configuration for the xunit runner. The only thing we really need is to disable shadow copy so that the tests run from the bin\Debug\net461 directory.

{
  "shadowCopy": false
}

The juicy workaround here is to get the IntegrationTestSite into the build output directory - that's needed in conjunction with disabling shadow copy.

@BrianVallelunga

This comment has been minimized.

Copy link

BrianVallelunga commented May 1, 2017

I can confirm that this fixes my issue in my main codebase. Thank you for the help and I would encourage this information to be publicized. I'm astounded more people haven't run into this.

@rynowak

This comment has been minimized.

Copy link
Member

rynowak commented May 4, 2017

We've made a decision to productize and harden the integration testing code that MVC is using to bootstrap our functional test websites. This will include validating common issues like the missing deps file. aspnet/Mvc#6233

I'm going to close this issue out.

@rynowak

This comment has been minimized.

Copy link
Member

rynowak commented May 17, 2017

Closing this issue since we're following up elsewhere

@fh-lchen

This comment has been minimized.

Copy link

fh-lchen commented Aug 9, 2017

I tried the fix and it is still giving me the same error. Can you share the sample project after it is fixed? Thanks!

@drauch

This comment has been minimized.

Copy link

drauch commented Aug 9, 2017

micbojhan pushed a commit to micbojhan/template that referenced this issue Aug 25, 2017

Implemented integration tests with a proper in memory TestServer and …
…database.

Also added black magic fix to make Startup from another project work. See aspnet/Razor#1212
A better way of doing this is coming in the future.
@Ciantic

This comment has been minimized.

Copy link

Ciantic commented Sep 9, 2017

I think the main problem is that Visual Studio 15.3.3 asp.net core 2.0 xunit template creates a csproj file that is already broken.

E.g. here is what it created in my project:

https://github.com/Ciantic/OksidiCom.AspNetCore/blob/fb11872b9e5ab9a015909dd34a76c737b05fbdce/test/OksidiCom.UserService.Test/OksidiCom.UserService.Test.csproj

And it gives the Razor compilation error in here:

https://github.com/Ciantic/OksidiCom.AspNetCore/blob/fb11872b9e5ab9a015909dd34a76c737b05fbdce/test/OksidiCom.UserService.Test/Tests.cs#L120 (LoginIssuesToken test)

If there is a simple fix by manipulating the csproj, the VS template for xunit test project should include it already.

@rynowak

This comment has been minimized.

Copy link
Member

rynowak commented Sep 11, 2017

Hi, it looks like you are posting on a closed issue/PR/commit!

We're very likely to lose track of your bug/feedback/question unless you:

  1. Open a new issue
  2. Explain very clearly what you need help with
  3. If you think you have found a bug, include detailed repro steps so that we can investigate the problem

Thanks!

@evil-shrike

This comment has been minimized.

Copy link
Contributor

evil-shrike commented Sep 25, 2017

@rynowak
I have the same issue (as many others who try to use TestServer I guess) but the suggested workaround isn't working for me.
I've added the mentioned Target in csproj, created a xunit.runner.json, but still having the following error:
(trimmed output)

Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: 'One or more compilation failures occurred:
ooebhccx.1bd(4,62): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
ooebhccx.1bd(4,81): error CS0518: Predefined type 'System.String' is not defined or imported
ooebhccx.1bd(4,112): error CS0518: Predefined type 'System.Type' is not defined or imported

The test projects targets netcore2.0. It references Microsoft.AspNetCore.All, Microsoft.AspNetCore.TestHost, and Microsoft.NET.Test.Sdk.

I checked that the output dir (bin\Debug\netcoreapp2.0) contains *.deps.json and xunit.runner.json.

The error happens even for empty views - I have errors even if View's content is just <!doctype html>.

@evil-shrike

This comment has been minimized.

Copy link
Contributor

evil-shrike commented Sep 25, 2017

Also please note that the issue was closed before .NET Core 2.0 was released. It could makes sense if it was going to be fixed in the upcoming release. But it's not. I'm having it in netcore2.0 rtm:

<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />

Please reopen the issue.

The mentioned issue aspnet/Mvc#6233 is also closed. So it's pretty unclear what developers are supposed to do or expect.

Here's a demo project - https://github.com/evil-shrike/aspnetcore-tests-bug

@pranavkm

This comment has been minimized.

Copy link
Member

pranavkm commented Sep 25, 2017

@evil-shrike you'll need to target the Web Sdk (Sdk="Microsoft.NET.Sdk.Web") to get the right kind of information stashed in the deps file. (alternatively you can set <PreserveCompilationContext>true</PreserveCompilationContext>)

@evil-shrike

This comment has been minimized.

Copy link
Contributor

evil-shrike commented Sep 25, 2017

@pranavkm such a tiny detail :) Setting PreserveCompilationContext has solved the problem. Thanks!
Shouldn't it be mentioned in the doc somewhere?

@pranavkm

This comment has been minimized.

Copy link
Member

pranavkm commented Sep 25, 2017

@evil-shrike that would help. Would you like to send a PR for it?

@evil-shrike

This comment has been minimized.

Copy link
Contributor

evil-shrike commented Sep 25, 2017

@pranavkm ok, I'll make a PR.
I'm trying to repeat the minimal number of steps to get it working and found that setting PreserveCompilationContext=true is enough (without adding Target, or xunit.runner.json). Could you confirm that it should be enough?

@pranavkm

This comment has been minimized.

Copy link
Member

pranavkm commented Sep 25, 2017

Yup, it's definitely the minimum feature to enable to get view compilation working.

@bgever

This comment has been minimized.

Copy link

bgever commented Sep 28, 2017

@rynowak's solution to add the manual step to copy .deps.json files worked for me, and the documented PreserveCompilationContext had no effect.

Details in this SO Q&A: https://stackoverflow.com/questions/46464373/asp-net-core-testserver-results-in-http-500-for-razor-views/46464374

@bgever

This comment has been minimized.

Copy link

bgever commented Feb 13, 2018

Good news, looks like a fix for this is on the ASP.NET Core 2.1 roadmap: (emphasis mine)

Functional testing infrastructure

Writing functional tests for an MVC app allows you to test handling of a request end-to-end including running routing, filters, controllers, actions, views and pages. While writing in-memory functional tests for MVC apps is possible with ASP.NET Core 2.0 it requires significant setup.

For 2.1 we will provide an test fixture implementation that handles the typical pitfalls when trying to test MVC applications using TestServer:

  • Copy the .deps file from your project into the test assembly bin folder
  • Specify the content root of the application’s project root so that static files and views can be found
  • Streamline setting up your app on TestServer
@drauch

This comment has been minimized.

Copy link

drauch commented Feb 13, 2018

@bgever : Optimally the solution will allow shadow copying

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