C# VS 2017 ASP.NET核心测试项目-缺少Microsoft.AspNetCore.Identity
我正在尝试使用Microsoft.AspNetCore.TestHost为空的.NET Core ASP站点编写集成测试 我的测试类与文档类相同,如下所示:C# VS 2017 ASP.NET核心测试项目-缺少Microsoft.AspNetCore.Identity,c#,.net,asp.net-core-mvc,xunit,visual-studio-2017,C#,.net,Asp.net Core Mvc,Xunit,Visual Studio 2017,我正在尝试使用Microsoft.AspNetCore.TestHost为空的.NET Core ASP站点编写集成测试 我的测试类与文档类相同,如下所示: public class UnitTest1 { private readonly TestServer _server; private readonly HttpClient _client; public UnitTest1() { // Arrange _server
public class UnitTest1
{
private readonly TestServer _server;
private readonly HttpClient _client;
public UnitTest1()
{
// Arrange
_server = new TestServer(new WebHostBuilder()
.UseContentRoot(ContentPath)
.UseStartup<Startup>());
_client = _server.CreateClient();
}
[Fact]
public async Task ReturnHelloWorld()
{
// Act
var response = await _client.GetAsync("/");
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
// Assert
Console.WriteLine("Test");
}
private static string ContentPath
{
get
{
var path = PlatformServices.Default.Application.ApplicationBasePath;
var contentPath = Path.GetFullPath(Path.Combine(path, $@"..\..\..\..\{nameof(DataTests)}"));
return contentPath;
}
}
}
Test.cs
有同样的问题。。经过一番挖掘,我找到了一个有效的解决办法 Razor中使用的roslyn编译器不包括主程序集的引用程序集。。 所以我通过查找来添加这些 在测试类中添加以下代码。。在我的机器上工作™
private static string ContentPath
{
get
{
var path = PlatformServices.Default.Application.ApplicationBasePath;
var contentPath = Path.GetFullPath(Path.Combine(path, $@"..\..\..\..\{nameof(src)}"));
return contentPath;
}
}
GitHub回购协议上的同一问题也有同样的问题。。经过一番挖掘,我找到了一个有效的解决办法 Razor中使用的roslyn编译器不包括主程序集的引用程序集。。 所以我通过查找来添加这些 在测试类中添加以下代码。。在我的机器上工作™
private static string ContentPath
{
get
{
var path = PlatformServices.Default.Application.ApplicationBasePath;
var contentPath = Path.GetFullPath(Path.Combine(path, $@"..\..\..\..\{nameof(src)}"));
return contentPath;
}
}
GitHub回购上也有同样的问题谢谢!这就解决了那个错误!现在我需要找出我需要引用的所有其他程序集添加了几个程序集,现在缺少:类型或命名空间名称“Html”在命名空间“Microsoft.AspNetCore”中不存在。是否缺少程序集引用?谢谢!这就解决了那个错误!现在我需要找出我需要引用的所有其他程序集添加了几个程序集,现在缺少:类型或命名空间名称“Html”在命名空间“Microsoft.AspNetCore”中不存在。是否缺少程序集引用?
public class UnitTest1
{
private readonly TestServer _server;
private readonly HttpClient _client;
private readonly ITestOutputHelper output;
public UnitTest1(ITestOutputHelper output)
{
this.output = output;
// Arrange
_server = new TestServer(new WebHostBuilder()
.ConfigureTestContent()
.ConfigureLogging(l => l.AddConsole())
.UseStartup<Startup>()
.ConfigureTestServices());
_client = _server.CreateClient();
}
[Fact]
public async Task ReturnHelloWorld()
{
// Act
var response = await _client.GetAsync("/");
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
// Assert
output.WriteLine(body);
}
}
private static string ContentPath
{
get
{
var path = PlatformServices.Default.Application.ApplicationBasePath;
var contentPath = Path.GetFullPath(Path.Combine(path, $@"..\..\..\..\{nameof(src)}"));
return contentPath;
}
}
var builder = new WebHostBuilder()
.UseContentRoot(ContentPath)
.ConfigureLogging(factory =>
{
factory.AddConsole();
})
.UseStartup<Startup>()
.ConfigureServices(services =>
{
services.Configure((RazorViewEngineOptions options) =>
{
var previous = options.CompilationCallback;
options.CompilationCallback = (context) =>
{
previous?.Invoke(context);
var assembly = typeof(Startup).GetTypeInfo().Assembly;
var assemblies = assembly.GetReferencedAssemblies().Select(x => MetadataReference.CreateFromFile(Assembly.Load(x).Location))
.ToList();
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("mscorlib")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Private.Corelib")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Razor")).Location));
context.Compilation = context.Compilation.AddReferences(assemblies);
};
});
});
_server = new TestServer(builder);