dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.39k stars 10k forks source link

VS live unit testing is incompatible with .razor compilation #13549

Closed SteveSandersonMS closed 2 years ago

SteveSandersonMS commented 5 years ago

If you have unit tests that try to render components compiled from .razor files, those tests may work fine under VS's normal unit test runner, but they won't work when running under "live unit testing". You'll find that the BuildRenderTree methods don't produce any output in that mode.

My guess is that, for whatever reason, VS is using the design-time builds when running under live unit tests, and therefore the BuildRenderTree methods are just empty.

If you want to repro this, try this repo which contains some simple unit tests. You should be able to see them pass under normal test runners, but fail under the live unit testing runner.

SteveSandersonMS commented 4 years ago

I've heard this is being treated as a bug on the VS side, since it affects not just Blazor but various other tech stacks and 3rd-party build customizations too.

When VS 16.7 ships, we should re-verify this. The problem might be gone.

poke commented 4 years ago

When VS 16.7 ships, we should re-verify this. The problem might be gone.

Is there a reason we are waiting for 16.7 on this? 16.6 isn’t out just yet.

TanayParikh commented 3 years ago

Looks like this is still an issue. I'm getting:

[16:11:01.236 Verbose] - BuildManager - System.IO.FileNotFoundException: Could not find file 'C:\Users\taparik\dev\BlazorUnitTestingPrototype\.vs\UnitTests\v17\lut\0\b\SampleApp\bin\Debug\netstandard2.0\SampleApp.dll'.
File name: 'C:\Users\taparik\dev\BlazorUnitTestingPrototype\.vs\UnitTests\v17\lut\0\b\SampleApp\bin\Debug\netstandard2.0\SampleApp.dll'
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at Roslyn.Utilities.StandardFileSystem.OpenFile(String filePath, FileMode mode, FileAccess access, FileShare share)
   at Roslyn.Utilities.CommonCompilerFileSystemExtensions.OpenFileWithNormalizedException(ICommonCompilerFileSystem fileSystem, String filePath, FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
   at Microsoft.CodeAnalysis.LiveUnitTesting.BuildManagement.Legacy.CompilationComparer.TryGetReference(CommandLineReference commandLineReference, Logger logger, MetadataReference& reference)
[16:11:01.273 Verbose] - BuildManager - C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\MyTests.cs(2,17): error CS0234: The type or namespace name 'Pages' does not exist in the namespace 'SampleApp' (are you missing an assembly reference?)
[16:11:01.273 Verbose] - BuildManager - C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\TodoListTest.cs(2,17): error CS0234: The type or namespace name 'Pages' does not exist in the namespace 'SampleApp' (are you missing an assembly reference?)
[16:11:01.288 Verbose] - BuildManager - C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\TodoListTest.cs(14,47): error CS0246: The type or namespace name 'TodoList' could not be found (are you missing a using directive or an assembly reference?)
[16:11:01.288 Verbose] - BuildManager - C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\TodoListTest.cs(23,47): error CS0246: The type or namespace name 'TodoList' could not be found (are you missing a using directive or an assembly reference?)
[16:11:01.288 Verbose] - BuildManager - C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\TodoListTest.cs(43,47): error CS0246: The type or namespace name 'TodoList' could not be found (are you missing a using directive or an assembly reference?)
[16:11:01.288 Verbose] - BuildManager - C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\MyTests.cs(15,47): error CS0246: The type or namespace name 'Counter' could not be found (are you missing a using directive or an assembly reference?)
[16:11:01.288 Verbose] - BuildManager - C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\MyTests.cs(33,47): error CS0246: The type or namespace name 'FetchData' could not be found (are you missing a using directive or an assembly reference?)
[16:11:01.288 Verbose] - BuildManager - C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\MyTests.cs(39,21): error CS0246: The type or namespace name 'FetchData' could not be found (are you missing a using directive or an assembly reference?)
[16:11:01.288 Verbose] - BuildManager - C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\MyTests.cs(40,21): error CS0246: The type or namespace name 'FetchData' could not be found (are you missing a using directive or an assembly reference?)
[16:11:01.288 Verbose] - BuildManager - C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(75,5): error MSB4181: The "Csc" task returned false but did not log an error.
[16:11:01.292 Verbose] - BuildManager - Some of the file paths defined in the csproj under the Live Unit Testing build path are missing. Verify whether these files should be present under the Live Unit Testing build path and if necessary update the csproj definition to look these files up in a different place.
[16:11:01.292 Verbose] - BuildManager - Item _OutputPathItem 'C:\Users\taparik\dev\BlazorUnitTestingPrototype\.vs\UnitTests\v17\lut\0\b\SampleApp.Tests\Debug\netcoreapp3.0\' evaluated via:
_OutputPathItem: '$(OutDir)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:310
  OutDir: '$(OutputPath)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:228
    OutputPath: '$(OutputPath)$(TargetFramework.ToLowerInvariant())\' @ C:\Program Files\dotnet\sdk\5.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets:242
      OutputPath: '$(BaseOutputPath)$(Configuration)\' @ C:\Program Files\dotnet\sdk\5.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.DefaultOutputPaths.targets:33
        BaseOutputPath: 'C:\Users\taparik\dev\BlazorUnitTestingPrototype\.vs\UnitTests\v17\lut\0\b\SampleApp.Tests\' @ C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\SampleApp.Tests.csproj:0
        Configuration: 'Debug'
[16:11:01.292 Verbose] - BuildManager - Item AppConfigFileDestination 'C:\Users\taparik\dev\BlazorUnitTestingPrototype\.vs\UnitTests\v17\lut\0\b\SampleApp.Tests\Debug\netcoreapp3.0\SampleApp.Tests.dll.config' evaluated via:
AppConfigFileDestination: '$(OutDir)$(TargetFileName).config' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:338
  OutDir: '$(OutputPath)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:228
    OutputPath: '$(OutputPath)$(TargetFramework.ToLowerInvariant())\' @ C:\Program Files\dotnet\sdk\5.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets:242
      OutputPath: '$(BaseOutputPath)$(Configuration)\' @ C:\Program Files\dotnet\sdk\5.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.DefaultOutputPaths.targets:33
        BaseOutputPath: 'C:\Users\taparik\dev\BlazorUnitTestingPrototype\.vs\UnitTests\v17\lut\0\b\SampleApp.Tests\' @ C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\SampleApp.Tests.csproj:0
        Configuration: 'Debug'
  TargetFileName: '$(TargetName)$(TargetExt)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:248
    TargetName: '$(AssemblyName)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:240
      AssemblyName: '$(MSBuildProjectName)' @ C:\Program Files\dotnet\sdk\5.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.props:32
        MSBuildProjectName: 'SampleApp.Tests'
    TargetExt: '.dll' @ C:\Program Files\dotnet\sdk\5.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.BeforeCommon.targets:99
[16:11:01.292 Verbose] - BuildManager - Item _DebugSymbolsOutputPath 'C:\Users\taparik\dev\BlazorUnitTestingPrototype\.vs\UnitTests\v17\lut\0\b\SampleApp.Tests\Debug\netcoreapp3.0\SampleApp.Tests.pdb' evaluated via:
_DebugSymbolsOutputPath: '@(_DebugSymbolsIntermediatePath->'$(OutDir)%(Filename)%(Extension)')' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:404
  OutDir: '$(OutputPath)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:228
    OutputPath: '$(OutputPath)$(TargetFramework.ToLowerInvariant())\' @ C:\Program Files\dotnet\sdk\5.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets:242
      OutputPath: '$(BaseOutputPath)$(Configuration)\' @ C:\Program Files\dotnet\sdk\5.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.DefaultOutputPaths.targets:33
        BaseOutputPath: 'C:\Users\taparik\dev\BlazorUnitTestingPrototype\.vs\UnitTests\v17\lut\0\b\SampleApp.Tests\' @ C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\SampleApp.Tests.csproj:0
        Configuration: 'Debug'
[16:11:01.292 Verbose] - BuildManager - Item _ApplicationManifestFinal 'C:\Users\taparik\dev\BlazorUnitTestingPrototype\.vs\UnitTests\v17\lut\0\b\SampleApp.Tests\Debug\netcoreapp3.0\Native.SampleApp.Tests.manifest' evaluated via:
_ApplicationManifestFinal: '$(OutDir)$(_DeploymentTargetApplicationManifestFileName)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:442
  OutDir: '$(OutputPath)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:228
    OutputPath: '$(OutputPath)$(TargetFramework.ToLowerInvariant())\' @ C:\Program Files\dotnet\sdk\5.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets:242
      OutputPath: '$(BaseOutputPath)$(Configuration)\' @ C:\Program Files\dotnet\sdk\5.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.DefaultOutputPaths.targets:33
        BaseOutputPath: 'C:\Users\taparik\dev\BlazorUnitTestingPrototype\.vs\UnitTests\v17\lut\0\b\SampleApp.Tests\' @ C:\Users\taparik\dev\BlazorUnitTestingPrototype\SampleApp.Tests\SampleApp.Tests.csproj:0
        Configuration: 'Debug'
  _DeploymentTargetApplicationManifestFileName: 'Native.$(AssemblyName).manifest' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:264
    AssemblyName: '$(MSBuildProjectName)' @ C:\Program Files\dotnet\sdk\5.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.props:32
      MSBuildProjectName: 'SampleApp.Tests'

My guess is that, for whatever reason, VS is using the design-time builds when running under live unit tests, and therefore the BuildRenderTree methods are just empty.

So I believe this should be fixed via dotnet/razor-tooling#7615.

1337jazz commented 2 years ago

Also getting this in VS 2021 17.1.0

HolisticDeveloper commented 2 years ago

Not sure if this is related or a different issue... In a Blazor Web Assembly solution, Live Unit Testing is failing to discover several tests. I found several compilation errors in the LUT build log, e.g.:

error CS0117: 'object' does not contain a definition for 'OnInitializedAsync'

I could clean most of these up by:

  1. Explicitly declaring Microsoft.AspNetCore.Components.ComponentBase as the base class in the .razor.cs file of my components.
  2. Ensure every .razor file had a corresponding .razor.cs file.
  3. Move all @code and @inject blocks to the code-behind files.

However, I'm still left with the following errors that I can't resolve:

[12:40:24 PM Verbose] - BuildManager - C:\Program Files\dotnet\sdk\6.0.300-preview.22204.3\Sdks\Microsoft.NET.Sdk.BlazorWebAssembly\targets\Microsoft.NET.Sdk.BlazorWebAssembly.6_0.targets(234,5): error : The asset 'C:\repos\kar-definition-manager\.vs\Kar.DefinitionManager\v17\lut\0\b\DefManager\Kar.DefinitionManager.Web\bin\Debug\net6.0\net6.0\wwwroot\_framework\Kar.DefinitionManager.Web.pdb' can not be found at any of the searched locations 'C:\repos\kar-definition-manager\.vs\Kar.DefinitionManager\v17\lut\0\b\DefManager\Kar.DefinitionManager.Web\bin\Debug\net6.0\net6.0\wwwroot\_framework\Kar.DefinitionManager.Web.pdb' and 'C:\repos\kar-definition-manager\.vs\Kar.DefinitionManager\v17\lut\0\i\Kar.DefinitionManager.Web\net6.0\Kar.DefinitionManager.Web.pdb'
[12:40:24 PM Verbose] - BuildManager - Some of the file paths defined in the csproj under the Live Unit Testing build path are missing. Verify whether these files should be present under the Live Unit Testing build path and if necessary update the csproj definition to look these files up in a different place.
[12:40:24 PM Verbose] - BuildManager - Item _OutputPathItem 'C:\repos\kar-definition-manager\.vs\Kar.DefinitionManager\v17\lut\0\b\DefManager\Kar.DefinitionManager.Web\bin\Debug\net6.0\' evaluated via:
_OutputPathItem: '$(OutDir)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:310
  OutDir: 'C:\repos\kar-definition-manager\.vs\Kar.DefinitionManager\v17\lut\0\b\DefManager\Kar.DefinitionManager.Web\bin\Debug\net6.0\' @ C:\repos\kar-definition-manager\DefManager\Kar.DefinitionManager.Web\Kar.DefinitionManager.Web.csproj:0
[12:40:24 PM Verbose] - BuildManager - Item AppConfigFileDestination 'C:\repos\kar-definition-manager\.vs\Kar.DefinitionManager\v17\lut\0\b\DefManager\Kar.DefinitionManager.Web\bin\Debug\net6.0\Kar.DefinitionManager.Web.dll.config' evaluated via:
AppConfigFileDestination: '$(OutDir)$(TargetFileName).config' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:339
  OutDir: 'C:\repos\kar-definition-manager\.vs\Kar.DefinitionManager\v17\lut\0\b\DefManager\Kar.DefinitionManager.Web\bin\Debug\net6.0\' @ C:\repos\kar-definition-manager\DefManager\Kar.DefinitionManager.Web\Kar.DefinitionManager.Web.csproj:0
  TargetFileName: '$(TargetName)$(TargetExt)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:248
    TargetName: '$(AssemblyName)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:240
      AssemblyName: '$(MSBuildProjectName)' @ C:\Program Files\dotnet\sdk\6.0.300-preview.22204.3\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.props:32
        MSBuildProjectName: 'Kar.DefinitionManager.Web'
    TargetExt: '.dll' @ C:\Program Files\dotnet\sdk\6.0.300-preview.22204.3\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.BeforeCommon.targets:106
[12:40:24 PM Verbose] - BuildManager - Item _DebugSymbolsOutputPath 'C:\repos\kar-definition-manager\.vs\Kar.DefinitionManager\v17\lut\0\b\DefManager\Kar.DefinitionManager.Web\bin\Debug\net6.0\Kar.DefinitionManager.Web.pdb' evaluated via:
_DebugSymbolsOutputPath: '@(_DebugSymbolsIntermediatePath->'$(OutDir)%(Filename)%(Extension)')' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:406
  OutDir: 'C:\repos\kar-definition-manager\.vs\Kar.DefinitionManager\v17\lut\0\b\DefManager\Kar.DefinitionManager.Web\bin\Debug\net6.0\' @ C:\repos\kar-definition-manager\DefManager\Kar.DefinitionManager.Web\Kar.DefinitionManager.Web.csproj:0
[12:40:24 PM Verbose] - BuildManager - Item _ApplicationManifestFinal 'C:\repos\kar-definition-manager\.vs\Kar.DefinitionManager\v17\lut\0\b\DefManager\Kar.DefinitionManager.Web\bin\Debug\net6.0\Kar.DefinitionManager.Web.dll.manifest' evaluated via:
_ApplicationManifestFinal: '$(OutDir)$(_DeploymentTargetApplicationManifestFileName)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:444
  OutDir: 'C:\repos\kar-definition-manager\.vs\Kar.DefinitionManager\v17\lut\0\b\DefManager\Kar.DefinitionManager.Web\bin\Debug\net6.0\' @ C:\repos\kar-definition-manager\DefManager\Kar.DefinitionManager.Web\Kar.DefinitionManager.Web.csproj:0
  _DeploymentTargetApplicationManifestFileName: '$(TargetFileName).manifest' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:268
    TargetFileName: '$(TargetName)$(TargetExt)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:248
      TargetName: '$(AssemblyName)' @ C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets:240
        AssemblyName: '$(MSBuildProjectName)' @ C:\Program Files\dotnet\sdk\6.0.300-preview.22204.3\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.props:32
          MSBuildProjectName: 'Kar.DefinitionManager.Web'
      TargetExt: '.dll' @ C:\Program Files\dotnet\sdk\6.0.300-preview.22204.3\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.BeforeCommon.targets:106
HolisticDeveloper commented 2 years ago

The New build experience for Live Unit Testing that is available as a preview feature in VS 17.2 has resolved this for me.

mkArtakMSFT commented 2 years ago

Looks like this has been resolved per @HolisticDeveloper 's comment above. Closing. Thanks @HolisticDeveloper !