Home > front end >  Visual Studio ~ Test project does not find namespace of tested project
Visual Studio ~ Test project does not find namespace of tested project

Time:12-17

I have a C# ASP.NET Core Project that I want to write tests for. However, for some strange reason the test project does not find the namespaces (and thus also the classes) of the project that I want to test. This notably happens even though I clearly have the main project referenced. How can this be?

The specific error I get is:

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

As mentioned before, this happens even though I did add the project reference to DeviceWebDisplay. In fact, VisualStudio even offers me the quick action "Add reference to 'DeviceWebDisplay'". However, regardless of whether I've already added this or not, clicking that quick action doesn't have any effect.

Does anyone have any idea what caused this? For reference, here's the templates I used for the two projects:

  • DeviceWebDisplay: ASP.NET Core Web App
  • DeviceWebDisplayTest: Unit Test Project (.NET Framework)

enter image description here

EDIT:

I figured it might be the Target framework of the test project, but for some reason that one only goes up to 6.0, and the download website (that I get to when I click "Install other frameworks...") does not offer any SDKs for Visual Studio 2022.

enter image description here

enter image description here

And the thing is, my visual studio clearly has .NET 6.0 installed, or otherwise I wouldn't be able to use it in my main project.

Could it maybe be that I need to use a different template for the test project?

CodePudding user response:

The problem is your project DeviceWebDisplay target .net 6 while your test project DeviceWebDisplayTest target .NETFramwork,Version=4.7.2

project which target .NETFramwork,Version=4.7.2 can not reference a project target .net6

you should Create a new Test project target .net 6

Read more about differance between .NET vs .NET Framework for server apps

CodePudding user response:

I found out what the problem was.

The basic cause was that I created my test project based on the "Unit Test Project (.NET Framework)" template. For some reason, that framework does not support .NET versions above 4.8

I now created a second test project based on the "MSTest Test Project" template instead, and there I was able to select version 6.0. Afterwards, I was also able to simply copy my test over from the previous test project, and then it worked just fine.

  • Related