Pure.Krome
Pure.Krome

Reputation: 87047

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:

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?

Upvotes: 2

Views: 8146

Answers (1)

snerting
snerting

Reputation: 380

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

Upvotes: 8

Related Questions