Integration testing ASP.NET Core with .NET Framework - can't find deps.json

别说谁变了你拦得住时间么 提交于 2020-12-29 05:29:59

问题


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.


回答1:


Follow steps below to create Integration Test for Asp.Net Core with targeting net 47.

  1. Create New Project-> xUnit Test Project(.Net Core)
  2. Right click new project->Edit .csproj->Change TargetFramework to net47
  3. Add Project Reference to TestRepro
  4. Install-Package Microsoft.AspNetCore.Mvc.Testing
  5. 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");
        }
    
    }
    
  6. Run Test Project




回答2:


I just ran into this same issue and found the root cause to be quite obscure. From the documentation, the .deps files should be copied to the integration test project's bin directory. This wasn't happening for me because was not explicitly referencing the Microsoft.AspNetCore.Mvc.Testing package from my integration test project. I had created a shared library with some utility functions that referenced that nuget package, so my integration test project indirectly referenced it.

There's some custom build tasks in the Microsoft.AspNetCore.Mvc.Testing package that copy the referenced service deps.json files for you, so you must reference it directly from the integration test project in order to get those build tasks to run.




回答3:


For me, I already had a reference to Microsoft.AspNetCore.Mvc.Test in my test project, but removing the following fixed the issue:

<GenerateAssemblyInfo>false</GenerateAssemblyInfo>



回答4:


Accepted answer assumes that deps.json file exists in your project bin folder, so it can be copied into integration test bin folder.
In my case deps.json file was missing in application bin folder. In this case make sure you have

<PreserveCompilationContext>true</PreserveCompilationContext>

in your csproj file.



来源:https://stackoverflow.com/questions/55131379/integration-testing-asp-net-core-with-net-framework-cant-find-deps-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!