При тестировании Asp.NetCore (2.2) с полной .NET Framework (4.7.2) с юнит-тестами (MSTest 1.3.2) с помощью Microsoft.AspNetCore.TestHost (2.2.0) NuGet, я получить следующее исключение
System.IO.FileNotFoundException: Could not load file or assembly 'System.Buffers, Version=4.0.2.0
Однако я выбрал Microsoft.AspNetCore.TestHost 2.2.0
и автоматически загрузил все зависимости. Следовательно, packages.json выглядит следующим образом:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Hosting" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Hosting.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Hosting.Server.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Http" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Http.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Http.Extensions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Http.Features" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.TestHost" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.WebUtilities" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.Binder" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.EnvironmentVariables" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.FileExtensions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.DependencyInjection" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.FileProviders.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.FileProviders.Physical" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.FileSystemGlobbing" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Hosting.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Logging" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.ObjectPool" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Options" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Primitives" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Net.Http.Headers" version="2.2.0" targetFramework="net472" />
<package id="MSTest.TestAdapter" version="1.3.2" targetFramework="net472" />
<package id="MSTest.TestFramework" version="1.3.2" targetFramework="net472" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net472" />
<package id="System.Buffers" version="4.5.0" targetFramework="net472" />
<package id="System.Collections.Immutable" version="1.5.0" targetFramework="net472" />
<package id="System.ComponentModel.Annotations" version="4.5.0" targetFramework="net472" />
<package id="System.Data.SQLite" version="1.0.109.2" targetFramework="net472" />
<package id="System.Data.SQLite.Core" version="1.0.109.2" targetFramework="net472" />
<package id="System.Data.SQLite.EF6" version="1.0.109.0" targetFramework="net472" />
<package id="System.Data.SQLite.Linq" version="1.0.109.0" targetFramework="net472" />
<package id="System.Diagnostics.DiagnosticSource" version="4.5.0" targetFramework="net472" />
<package id="System.IO.Pipelines" version="4.5.2" targetFramework="net472" />
<package id="System.Memory" version="4.5.1" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net472" />
<package id="System.Reflection.Metadata" version="1.6.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.1" targetFramework="net472" />
<package id="System.Text.Encodings.Web" version="4.5.0" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.1" targetFramework="net472" />
</packages>
Тест-класс
[TestInitialize]
public void TestInitialize()
{
server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
Client = server.CreateClient();
}
private HttpClient Client { get; set; }
[TestMethod]
public async Task GetDatabaseId()
{
var response = await Client.GetAsync("api/values");
;
}
При отладке теста исключение "... System.Buffers, ..." генерируется в строке app.UseMvc();
в Startup.cs (сгенерированный по умолчанию класс с нулевыми изменениями)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseMvc();
}