Steve Wortham
Steve Wortham

Reputation: 22230

Unit Test Project can't reference MVC project

Perhaps this is easier if you let Visual Studio create the Unit Test project when it creates the MVC project. But seeing as how that wasn't done, how should I add a Unit Test project now?

Here's what I did:

  1. Right-click solution and click Add Project.
  2. Browse to Visual C# > Test and add a Unit Test Project.
  3. Within the Unit Test Project, I click Add Reference and then choose the MVC 5 project.

After all of that, I see a little yellow hazard icon on the newly added reference, and I can't access any of its namespaces in the code.

Upvotes: 10

Views: 7649

Answers (4)

Rymnel
Rymnel

Reputation: 4515

Using the Solution Explorer, browse your test Solution for References: SolutionExplorer

Right Click on "References"

Select Add Reference

Choose Solution from the menu on the left side and confirm that your primary project is checked. Like so: enter image description here

That did it for me in VS 2013!

Upvotes: 1

rahul
rahul

Reputation: 11

This problem also exists when there path is not defined correctly so you should browse these Reference very carefully.

GoTo=>Reference=>Add Reference=>Click Browse(btn)=>Select Reference then Ok

Upvotes: 1

Steve Wortham
Steve Wortham

Reputation: 22230

I figured it out. I forgot that there will be a warning message that goes along with the little yellow warning icon on a reference. So I went to the Error List and enabled "Warnings" in the filter. There was a message about mismatched .NET versions. The unit test project was using .NET 4.5 and the MVC project was 4.5.1. I changed the unit test project to 4.5.1, and voila, it works.

Upvotes: 30

TGH
TGH

Reputation: 39258

My theory is that you have to install MVC in the test project in order to satisfy the requirements. You can try to add the following nuget package from the package manager console to the unit tests project to intall MVC

Install-Package Microsoft.AspNet.Mvc

Alternatively you can use the Nuget UI manager in Visual Studio

Upvotes: 6

Related Questions