无法在我的 .NET Core 单元测试项目中加载文件或程序集 Microsoft.AspNetCore.Mvc.ApiExplorer [英] Could not load file or assembly Microsoft.AspNetCore.Mvc.ApiExplorer in my .NET Core unit test project

查看:67
本文介绍了无法在我的 .NET Core 单元测试项目中加载文件或程序集 Microsoft.AspNetCore.Mvc.ApiExplorer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .NET Core 解决方案中有两个项目:

I have two projects in a .NET Core solution:

  • TestWebApp(典型的 ASP.NET Core 2.2 Web 应用程序)
  • 单元测试应用

我的单元测试应用程序在运行期间抱怨缺少许多文件.我想知道为什么当我认为应该通过 TestWebApp 提供/提供这些文件时却丢失了这些文件.

My unit test application is complaining during runtime that there are a number of files missing. I'm wondering why these files are missing when I thought they should be provided/available via the TestWebApp.

这是 csproj 文件:

Here's the csproj files:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Hellang.Middleware.ProblemDetails" Version="3.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

....

单元测试项目/应用

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>

    <IsPackable>false</IsPackable>

    <LangVersion>latest</LangVersion>
  </PropertyGroup>

  ....

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

   ...

这是我现在运行任何单元测试时遇到的第一个错误:

Here's the first error I get when I run any unit test, now:

消息:System.IO.FileNotFoundException:无法加载文件或程序集Microsoft.AspNetCore.Mvc.ApiExplorer,版本=2.2.0.0,Culture=neutral,PublicKeyToken=adb9793829ddae60".系统找不到指定的文件

Message: System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore.Mvc.ApiExplorer, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified

然后我手动将它添加到单元测试项目中,重新编译并运行所有测试,现在我得到这个运行时错误:

So then I manually nuget add this to the unit test project, recompile and run all tests and now I get this runtime error:

消息:System.IO.FileNotFoundException:无法加载文件或程序集Microsoft.AspNetCore.Razor.Runtime,版本=2.2.0.0,Culture=neutral,PublicKeyToken=adb9793829ddae60".系统找不到指定的文件.

Message: System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore.Razor.Runtime, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

所以现在它找不到另一个文件..我认为它应该通过TestWebApp下拉?

So now it can't find another file .. which I thought it should have pulled down via the TestWebApp ?

Nuget 添加该文件...编译...并测试一切正常.

Nuget add that file ... compile .. and tests all run OK.

那么为什么我需要手动将这两个 nuget 添加到 xunit 测试项目中?

So why did I need to manually add both of those nugets to the xunit test project?

推荐答案

遇到了同样的问题,但是阅读了 测试本文档中的应用程序先决条件部分 工作正常.

Had the same problem, but after reading Test App Prerequisites part in this document it was working.

我在项目文件中遗漏的是:

What I had missed out in the project file was:

使用

<Project Sdk="Microsoft.NET.Sdk.Web"> instead of <Project Sdk="Microsoft.NET.Sdk"> at the top.

并使用了以下软件包:

<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />

这篇关于无法在我的 .NET Core 单元测试项目中加载文件或程序集 Microsoft.AspNetCore.Mvc.ApiExplorer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆