VS 2017 ASP.NET 核心测试项目 - Microsoft.AspNetCore.Identity 缺失
VS 2017 ASP.NET Core Test Project - Microsoft.AspNetCore.Identity missing
我正在尝试使用 Microsoft.AspNetCore.TestHost
为空的 .NET Core ASP 站点编写集成测试
Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException :
One or more compilation failures occurred:
/Views/_ViewImports.cshtml(5,28):
error CS0234: The type or namespace name 'Identity'
does not exist in the namespace 'Microsoft.AspNetCore'
(are you missing an assembly reference?) 4uvgaffv.11j(34,11):
error CS0246: The type or namespace name 'System' could not be found
(are you missing a using directive or an assembly reference?)
我的测试 class 与文档相同,如下所示:
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;
}
}
}
我尝试将 Microsoft.AspNetCore.Identity 1.1.1
NuGet 包添加到测试项目(与 MVC 项目相同)但它没有做任何事情,尽管我可以在 Dependencies
下拉列表中看到它丢失:
我已经尝试重新安装这些软件包,dotnet build
,dotnet restore
,完全重建但仍然没有成功。
有什么想法吗?
修复
对此的最终修复是(感谢@Jeffrey
WebHostBuilderExtensions.cs
public static class WebHostBuilderExtensions
{
private static string ContentPath
{
get
{
var path = PlatformServices.Default.Application.ApplicationBasePath;
var contentPath = Path.GetFullPath(Path.Combine(path, $@"..\..\..\..\{nameof(DataTests)}"));
return contentPath;
}
}
public static IWebHostBuilder ConfigureTestContent(this IWebHostBuilder builder)
{
return builder.UseContentRoot(ContentPath);
}
public static IWebHostBuilder ConfigureTestServices(this IWebHostBuilder builder)
{
return builder.ConfigureServices(services =>
{
services.AddMvcCore();
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.ApplicationInsights.AspNetCore")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Html.Abstractions")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Razor")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Razor.Runtime")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Mvc")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Dynamic.Runtime")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Text.Encodings.Web")).Location));
context.Compilation = context.Compilation.AddReferences(assemblies);
};
});
services.AddApplicationInsightsTelemetry();
});
}
}
Test.cs
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);
}
}
有同样的问题。经过一些挖掘找到了一个可行的解决方案。
Razor 中使用的 roslyn 编译器不包括主程序集的引用程序集。
所以我通过查找添加了这些
在测试中 class 添加以下代码.. 在我的机器上工作™
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);
上的相同问题
我正在尝试使用 Microsoft.AspNetCore.TestHost
Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException :
One or more compilation failures occurred:
/Views/_ViewImports.cshtml(5,28):
error CS0234: The type or namespace name 'Identity'
does not exist in the namespace 'Microsoft.AspNetCore'
(are you missing an assembly reference?) 4uvgaffv.11j(34,11):
error CS0246: The type or namespace name 'System' could not be found
(are you missing a using directive or an assembly reference?)
我的测试 class 与文档相同,如下所示:
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;
}
}
}
我尝试将 Microsoft.AspNetCore.Identity 1.1.1
NuGet 包添加到测试项目(与 MVC 项目相同)但它没有做任何事情,尽管我可以在 Dependencies
下拉列表中看到它丢失:
我已经尝试重新安装这些软件包,dotnet build
,dotnet restore
,完全重建但仍然没有成功。
有什么想法吗?
修复
对此的最终修复是(感谢@Jeffrey
WebHostBuilderExtensions.cs
public static class WebHostBuilderExtensions
{
private static string ContentPath
{
get
{
var path = PlatformServices.Default.Application.ApplicationBasePath;
var contentPath = Path.GetFullPath(Path.Combine(path, $@"..\..\..\..\{nameof(DataTests)}"));
return contentPath;
}
}
public static IWebHostBuilder ConfigureTestContent(this IWebHostBuilder builder)
{
return builder.UseContentRoot(ContentPath);
}
public static IWebHostBuilder ConfigureTestServices(this IWebHostBuilder builder)
{
return builder.ConfigureServices(services =>
{
services.AddMvcCore();
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.ApplicationInsights.AspNetCore")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Html.Abstractions")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Razor")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Razor.Runtime")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Mvc")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Dynamic.Runtime")).Location));
assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Text.Encodings.Web")).Location));
context.Compilation = context.Compilation.AddReferences(assemblies);
};
});
services.AddApplicationInsightsTelemetry();
});
}
}
Test.cs
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);
}
}
有同样的问题。经过一些挖掘找到了一个可行的解决方案。
Razor 中使用的 roslyn 编译器不包括主程序集的引用程序集。 所以我通过查找添加了这些
在测试中 class 添加以下代码.. 在我的机器上工作™
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);
上的相同问题