Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't add Shared Project to Visual Studio Test Project

I'm using Visual Studio 2015

I have a Shared Project as an Independent Solution (A collection of Extensions used in several others Solutions).

I want to TEST the Shared Project (Independently any other solution).

So, I add a new TEST Project.

But, in my TEST Project I can't add ANY reference to the Shared Project (No option).

enter image description here

like image 495
RamonEeza Avatar asked Sep 17 '15 08:09

RamonEeza


People also ask

How do I add a reference to a shared project?

You add references to a shared project just as you would a normal project reference. In Visual Studio or Fire, you right-click the "References" node of the real project and choose "Add Reference", and then pick the shared project from the list.

Should unit tests be in the same project?

Put Unit tests in the same project as the code to achieve better encapsulation. You can easily test internal methods, which means you wont make methods public that should have been internal.


2 Answers

You can edit the project's csproj file and at end of imports add an entry like

<Import Project="..\SharedProject\SharedProject.projitems" Label="Shared" />

changing path & SharedProject as appropriate

probably best adding it to another project and copying the row created there (assuming same path depths)

like image 70
John M Avatar answered Sep 19 '22 06:09

John M


Since a shared project can't be compiled on its own it cannot be added as a reference directly to a unit test project.

However, a work-around I've used is to create a helper class library and use that as a reference in the test project. In your case, create a new class library and call it RobHelperTest.Helper. This project won't contain any code on its own, so delete the class1.cs file. Since this is a class library, you can reference shared projects so simply add a reference to all your shared projects to RobHelperTest.Helper and then create a reference to RobHelperTest.Helper in your unit test project RobHelperTest.

Screenshot showing the setup

Now you have full access to all your objects within the shared projects.

like image 33
GeirR Avatar answered Sep 21 '22 06:09

GeirR