我正在使用Target Famrwework 4.6对asp.net核心API进行集成测试,因为该项目需要我的一些旧的4.6 dll。我收到以下错误。
消息:System.Reflection.ReflectionTypeLoadException:无法加载一个或多个请求的类型。检索LoaderExceptions属性以获取更多信息。
当我调用await _client.GetAsync 时出现错误
我的测试项目是一个类库框架4.6。
TestFixture.cs
public class TestFixture :IDisposable
{
private const string SolutionName = "Project.API.sln";
private readonly TestServer _server;
public HttpClient Client { get; }
public TestFixture()
{
Assembly startAssembly = typeof(Startup).GetTypeInfo().Assembly;
var contentRoot = GetProjectPath(startAssembly);
var builder = new WebHostBuilder()
.UseContentRoot(contentRoot)
.UseEnvironment("Development")
.UseStartup<Startup>();
_server = new TestServer(builder);
Client = _server.CreateClient();
Client.BaseAddress = new Uri("http://localhost:62455");
}
public string GetProjectPath(Assembly startupAssembly)
{
var projectName = startupAssembly.GetName().Name;
var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath;
DirectoryInfo directoryInfo = new DirectoryInfo(applicationBasePath);
do
{
var solutionFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, SolutionName));
if (solutionFileInfo.Exists)
{
return Path.GetFullPath(Path.Combine(directoryInfo.FullName, projectName));
}
directoryInfo = directoryInfo.Parent;
}
while (directoryInfo.Parent != null);
throw new Exception($"Solution root could be located using application root {applicationBasePath}");
}
public void Dispose()
{
Client.Dispose();
_server.Dispose();
}
}
test.cs中
public class TransactionControllerTest :IClassFixture<TestFixture>
{
private string businessID = "1206";
private readonly HttpClient _client;
public TransactionControllerTest(TestFixture fixture)
{
_client = fixture.Client;
}
[Fact]
public async void ProcessFinalTransactionProcess()
{
var response = await _client.GetAsync($"/v1/businesses/{businessID}/transactions/TestMe");
Assert.NotNull(response);
}
}