在ASP中设置视图引擎.NET MVC6在单元测试中与asp.net . testhost . testserver一起
本文关键字:asp net testserver 一起 testhost MVC6 设置 ASP 视图 引擎 NET | 更新日期: 2023-09-27 18:15:51
如何在ASP.NET MVC 6
中设置视图引擎以与TestServer
创建的测试主机一起工作。我已经尝试从MVC 6
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;
}
启动类只是MVC最简单的设置:
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add MVC services to the services container.
services.AddMvc();
}
// Configure is called after ConfigureServices is called.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Add MVC to the request pipeline.
app.UseMvc(routes =>
{
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
}
}
我得到Response status code does not indicate success: 500 (Internal Server Error)
和内部它无法找到索引。cshtml视图。所有路径下面是单元测试库路径或dnx路径:
var applicationBasePath = _appEnvironment.ApplicationBasePath;
var webRootPath = _env.WebRootPath;
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
如何设置视图引擎和环境从使用TestServer
的UnitTests工作?
你定义一个不同的环境的想法是正确的。
我看到了一个非常优雅的解决方案,通过使用扩展方法:
https://github.com/bartmax/TestServerMvcHelper它甚至在NuGet上,但我不能从那里得到它。也许你可以合并两个类 mvctestapplicationenvironments .cs和WebHostBuilderExtensions.cs到你的解决方案。
然后你可以使用下面的命令设置TestServer:var builder = TestServer.CreateBuilder();
builder.UseStartup<Startup>()
.UseEnvironment("Testing")
.UseApplicationPath("YourMvcApplication");
var server = new TestServer(builder);