C# 在ASP.NET MVC6中设置视图引擎,以便在单元测试中与AspNet.TestHost.TestServer一起工作
如何在C# 在ASP.NET MVC6中设置视图引擎,以便在单元测试中与AspNet.TestHost.TestServer一起工作,c#,asp.net-mvc,unit-testing,asp.net-core-mvc,viewengine,C#,Asp.net Mvc,Unit Testing,Asp.net Core Mvc,Viewengine,如何在ASP.NET MVC 6中设置视图引擎,以使用TestServer创建的测试主机。我已经尝试实现了frommvc6repo: [Fact] public async Task CallMvc() { var client = GetTestHttpClient(); //call to HomeController.Index to get Home/Index.cshtml content HttpRequestMessage request = new Ht
ASP.NET MVC 6
中设置视图引擎,以使用TestServer
创建的测试主机。我已经尝试实现了frommvc6
repo:
[Fact]
public async Task CallMvc()
{
var client = GetTestHttpClient();
//call to HomeController.Index to get Home/Index.cshtml content
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "/");
var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
PAssert.IsTrue(() => content != null);
}
private HttpClient GetTestHttpClient(Action<IServiceCollection> configureServices = null)
{
var applicationServices = CallContextServiceLocator.Locator.ServiceProvider;
var applicationEnvironment = applicationServices.GetRequiredService<IApplicationEnvironment>();
var libraryManager = applicationServices.GetRequiredService<ILibraryManager>();
var startupAssembly = typeof(Startup).Assembly;
var applicationName = startupAssembly.GetName().Name;
var library = libraryManager.GetLibraryInformation(applicationName);
var applicationRoot = Path.GetDirectoryName(library.Path);
var hostingEnvironment = new HostingEnvironment()
{
WebRootPath = applicationRoot
};
var loggerFactory = new LoggerFactory();
var startup = new Startup();
Action<IServiceCollection> configureServicesAction = services =>
{
services.AddInstance(applicationEnvironment);
services.AddInstance<IHostingEnvironment>(hostingEnvironment);
// Inject a custom assembly provider. Overrides AddMvc() because that uses TryAdd().
var assemblyProvider = new FixedSetAssemblyProvider();
assemblyProvider.CandidateAssemblies.Add(startupAssembly);
services.AddInstance<IAssemblyProvider>(assemblyProvider);
startup.ConfigureServices(services);
};
Action<IApplicationBuilder> configureApp = _ => startup.Configure(_, hostingEnvironment, loggerFactory);
var server = TestServer.Create(configureApp, configureServicesAction);
var httpClient = server.CreateClient();
return httpClient;
}
我得到的响应状态代码不表示成功:500(内部服务器错误)
并且在内部无法找到Index.cshtml视图。所有路径
以下是以下单元测试库路径或dnx路径:
var applicationBasePath = _appEnvironment.ApplicationBasePath;
var webRootPath = _env.WebRootPath;
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
使用
TestServer
从单元测试设置视图引擎和环境的方法是什么?您定义不同环境的想法是正确的
通过使用扩展方法,我看到了一个非常优雅的解决方案:
它甚至在NuGet上,但我无法从那里开始工作。很可能,您可以将两个类MvcTestApplicationEnvironment.cs和WebHostBuilderExtensions.cs合并到您的解决方案中
然后,您可以使用以下方法设置TestServer:
var builder = TestServer.CreateBuilder();
builder.UseStartup<Startup>()
.UseEnvironment("Testing")
.UseApplicationPath("YourMvcApplication");
var server = new TestServer(builder);
var builder=TestServer.CreateBuilder();
builder.UseStartup()
.UseEnvironment(“测试”)
.UseApplicationPath(“YourMVCaplication”);
var服务器=新的测试服务器(构建器);
var builder = TestServer.CreateBuilder();
builder.UseStartup<Startup>()
.UseEnvironment("Testing")
.UseApplicationPath("YourMvcApplication");
var server = new TestServer(builder);