Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach unit tests to ASP.NET Core project

I created an ASP.NET Core Web Application in VS 2015. The Add unit tests checkbox was available for the ASP.NET 4.x templates, but when I selected ASP.NET 5 templates it was grayed out. I tried creating the project anyway with plans to add my own unit tests.

I added another project to the solution but I'm not able to link the two. When I right-click the references of the UnitTest project and hit Add Reference, I can see the original project as an option.

But when I check the box and hit OK, I get a dialog box that says, "A reference to 'PangolinWeb' could not be added. An assembly must have a 'dll' or 'exe' extension in order to be referenced."

Why can't I add an ASP.NET Core project as a reference? Is this the only way to make all of its classes and methods available to my UnitTest project?

like image 948
icanfathom Avatar asked May 03 '16 19:05

icanfathom


Video Answer


2 Answers

The project still compiles to a dll that you can reference, so instead of referencing the project, reference the dll in the bin folder of the Core project

  1. Add a reference to the file using the Add Reference, browse dialog

  2. Edit the csproj to use a variable for the configuration so it builds in release mode correctly. e.g.

`

<Reference Include="YourLibrary, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\YourLibrary\bin\$(Configuration)\net45\YourLibrary.dll</HintPath>
  </Reference>
  1. use the solution project build order to make the referencing project build after the dotnetcore library project.
like image 169
Sirentec Avatar answered Sep 24 '22 18:09

Sirentec


You cannot add a ASP.NET 5 (ASP.NET Core) project as a reference to a .NET Framework .csproj project (which I presume your unit test project is). This is a feature gap in the current Visual Studio tooling but will be fixed (IMHO as part of the upcoming RC2). Why? Because it is not implemented ;).

I also want to highlight that the answer of Juliano is right. xUnit is the framework of choice, by the .NET and ASP.NET teams.

Solution ideas: Include the classes as a linked file into your csproj. Like that you can compile it twice and test it once. Not the finest solution but a workaround for a while till the tooling will catch up.

like image 22
Thomas Avatar answered Sep 22 '22 18:09

Thomas