Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly Microsoft.AspNetCore.Mvc.ApiExplorer in my .NET Core unit test project

I have two projects in a .NET Core solution:

  • TestWebApp (typical ASP.NET Core 2.2 web application)
  • Unit test app

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.

Here's the csproj files:

TestWebApp

<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>

....

Unit test project/app

<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:

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:

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.

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

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

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

like image 501
Pure.Krome Avatar asked Dec 08 '25 09:12

Pure.Krome


1 Answers

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:

Use

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

And used the following packages:

<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
like image 171
snerting Avatar answered Dec 11 '25 00:12

snerting



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!