I have a ASP.NET Core Web API project targeting .NET Framework 4.7 that I'm trying to write integration tests for. I created a unit test project using Visual Studio Add new project and then the Unit Test Project (.NET Framework) template. I added the Microsoft.AspNetCore.Mvc.Testing NuGet package to the test project, and I have the following test:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestRepro.Tests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public async Task TestMethod1()
{
var factory = new WebApplicationFactory<Startup>();
var client = factory.CreateClient();
var response = await client.GetAsync("/api/values");
}
}
}
But this throws the following exception:
Test method TestRepro.Tests.UnitTest1.TestMethod1 threw exception: System.InvalidOperationException: Can't find'[path removed]\TestRepro.Tests\bin\Debug\TestRepro.deps.json'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g 'true'. For functional tests to work they need to either run from the build output folder or the TestRepro.deps.json file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run.
I have verified that TestRepro.deps.json exists in the web application output folder (TestRepro\bin\Debug\net47), but it is not copied to the test project output folder (TestRepro.Tests\bin\Debug). And I have not been able to find out how to disable shadow copying.
Edit: The documentation says:
The Microsoft.AspNetCore.Mvc.Testing package handles the following tasks: Copies the dependencies file (*.deps) from the SUT into the test project's bin folder.
But that doesn't seem to work. I can copy the file manually, but that doesn't work in a automated build scenario. One way would be to have a build step doing it in TeamCity, but it feels crude. Any ideas?
I have a repro on GitHub if that helps.
Follow steps below to create Integration Test for Asp.Net Core with targeting net 47.
New Project-> xUnit Test Project(.Net Core)
TargetFramework
to net47
TestRepro
Microsoft.AspNetCore.Mvc.Testing
Add Test file like below
public class BasicTests
: IClassFixture<WebApplicationFactory<Startup>>
{
private readonly WebApplicationFactory<Startup> _factory;
public BasicTests(WebApplicationFactory<Startup> factory)
{
_factory = factory;
}
[Fact]
public async Task TestMethod1()
{
var client = _factory.CreateClient();
var response = await client.GetAsync("/api/values");
}
}
Run Test Project