5

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?

Will Ray
  • 10,621
  • 3
  • 46
  • 61
icanfathom
  • 291
  • 4
  • 11
  • Check out [this answer](http://stackoverflow.com/a/31751864/4270650). – Will Ray May 04 '16 at 14:02
  • I am working with ASP.NET Core now for 1 year. In my current project my team is using xUnit, which I think is a better choice for ASP.NET Core. For more details about it, check https://xunit.github.io/ – Juliano Sales May 03 '16 at 19:35

2 Answers2

13

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.
mcintyre321
  • 12,996
  • 8
  • 66
  • 103
m0r6aN
  • 850
  • 1
  • 11
  • 19
3

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.

Thomas
  • 5,080
  • 27
  • 42
  • Does anybody have an update on the timeline for when this will be fixed? – Lars Kemmann Oct 24 '16 at 18:37
  • 2
    The project was delayed, since they decided to scrap the project.json. They switch to csproj and mscorlib shim support with .NET Core 1.2. ETA Summer 2017 – Thomas Oct 26 '16 at 15:16