简单的MVC项目,使用xunit进行集成测试,验证返回的视图。结果出现了大量的异常,类似于
...
/Views/User/Index.cshtml(143,20): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
...
这是升级到vs2017之后,才出现的问题。
在测试项目的csproj中加入下面<Project>
之间配置
<Project>
...
<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>
然后还要在项目中添加一个xunit
的配置文件xunit.runner.json
,内容如下:
{
"shadowCopy": false
}
感谢rynowak
这个问题在asp.net core2.0中依然存在,报错如下:
An unhandled exception has occurred while executing the request
Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: One or more compilation failures occurred:
s0tm2wsq.eci(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'.
...
这个方法依然管用。
错误不是很明显吗,让你添加引用
添加了引用也无济于事,而且是没必要额外添加System.Runtime
。
问题已解决