1

I created an asp.net core mvc project and also created a unit testing project below is the code for the unit testing project:

using System;
using Microsoft.AspNetCore.Mvc;  // error, type or namespace is missing
using Xunit;

namespace Project.Tests
{
    public class UnitTest1
    { 

        [Fact]
        public void Test1()
        {
          ...
        }
    }
}

I don't quite understand, I used to create unit testing projects without any problem, why I cannot access Microsoft.AspNetCore.Mvc this time?

0

2 Answers 2

1

You can not access Microsoft.AspNetCore.Mvc in unit testing project because unit testing project is a separate project and it does not contain library Microsoft.AspNetCore.Mvc in it.

To fix this issue add reference of this assembly to your project.

To add Microsoft.AspNetCore.Mvc assembly to your unit test project:

  • Right click on references of your unit test project

enter image description here

  • Click on Manage NuGet Packages, Search for Microsoft.AspNetCore.Mvc and install it

enter image description here

Do you really need this assembly reference in unit test project?

  • If not, then remove its reference from UnitTest1 class
Sign up to request clarification or add additional context in comments.

2 Comments

How? did my answer help? have you added assembly to you testing project?
This answer will only provide partial solution. When dealing with .NET Core 3.1 and later, adding nuget ref of AspNet.Mvc won't work, especially when the namespace changed from NET Core 3.1 to 5.0 and later.
0

If you are creating the unit test based on .NET Core 3.0/3.1 on VS 2019 update 16.4.0 or later, you could just add FrameworkReference in the csproj of your unit test and include the FrameworkReference of ASP.NET Core. No need to reference nuget of ASP.NET Core.

Adding FrameworkReference in your csproj with ASP.NET Core reference is quite easy, for example:

<ItemGroup>
   <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

For more information about FrameworkReference, see also https://learn.microsoft.com/en-us/dotnet/core/compatibility/aspnetcore

1 Comment

I found this answer along with an answer to similar question helpful: stackoverflow.com/questions/55408993/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.