首页 新闻 赞助 找找看

升级到VS2017(csproj)之后,用xunit进行集成测试,在Razor视图中报错。

0
[已解决问题] 解决于 2017-05-08 08:13

简单的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'.
...
蝌蝌的主页 蝌蝌 | 初学一级 | 园豆:158
提问于:2017-05-06 12:40
< >
分享
最佳答案
0

这是升级到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

蝌蝌 | 初学一级 |园豆:158 | 2017-05-07 14:39

这个问题在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'.
...

这个方法依然管用。

蝌蝌 | 园豆:158 (初学一级) | 2017-08-23 11:08
其他回答(1)
0

 错误不是很明显吗,让你添加引用

Bluto | 园豆:317 (菜鸟二级) | 2017-05-06 19:05

添加了引用也无济于事,而且是没必要额外添加System.Runtime

支持(0) 反对(0) 蝌蝌 | 园豆:158 (初学一级) | 2017-05-07 08:59

问题已解决

支持(0) 反对(0) 蝌蝌 | 园豆:158 (初学一级) | 2017-05-08 08:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册