Duplicate 'System.Reflection.Assembly.*' attribute and type not found in ASP.NET Core MVC (xUnit Test)

153 views Asked by At

I have created an ASP.NET Core 6 MVC web application. Build succeeds without any errors in the project.

In the same solution, I created a xUnit test project as you can see the folder structure below:

FolderStructure

The unit test file code is:

using Microsoft.AspNetCore.Mvc;
using NetCoreMvcWebApp.Controllers;

namespace TestMvcApp
{
    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
            HomeController h = new HomeController(null);
            var res = (ViewResult)h.Index();
            var outdata = res.ViewData["SomeData"];
            Assert.IsType<DateTime>(outdata);
        }
    }
}

Added the ASP.NET Core MVC web application as a project reference to the test project dependencies.

When I build the solution or test project, I get these errors without any squiggly lines in the code files:

Error CS0246
The type or namespace name 'Fact' could not be found (are you missing a using directive or an assembly reference?) NetCoreMvcWebApp

Error CS0246
The type or namespace name 'FactAttribute' could not be found (are you missing a using directive or an assembly reference?) NetCoreMvcWebApp

Error CS0579
Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute' attribute NetCoreMvcWebApp

ErrorList

How to resolve these errors?

1

There are 1 answers

0
sachita iyer On BEST ANSWER

While creating the solution (Suppose .NET Core Web App) in the Visual Studio, it will ask you to check/uncheck the box of "Place Solution and project in same directory".

  • do not check it so that your solution and project will come in a different folder structure and whatever the project you'll add to that solution file can be added as individual projects.

  • You can add the other projects as a reference to the new projects added to the solution file.

Now, Create the ASP.NET Core 6 MVc App from the Scratch in Visual studo and remember to uncheck the option of "Placing sln and proj in the same directory"

enter image description here

Then add the xUnit Test project to the same solution file where you'll not get any type of errors.