2015-10-02 15 views
3

如何設置視圖引擎在ASP.NET MVC 6TestServer創建測試主機工作。我試圖實現從trick回購MVC 6設置在ASP.NET MVC6視圖引擎在單位與AspNet.TestHost.TestServer工作測試

[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?}"); 
     }); 
    } 
} 

我越來越​​並在內部它無法找到索引。 cshtml視圖。所有路徑 下面下面的單元測試庫路徑或DNX路徑:

var applicationBasePath = _appEnvironment.ApplicationBasePath; 
var webRootPath = _env.WebRootPath; 
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; 

什麼方式來設置視圖引擎和環境,從使用TestServer單元測試工作?

回答

0

你定義不同環境的想法是要在正確的方向。

我已經看到了這一個相當完美的解決方案通過使用擴展方法:

https://github.com/bartmax/TestServerMvcHelper

它甚至上的NuGet,但我不能讓它從那裏工作。 Propably您可以將兩個類MvcTestApplicationEnvironment.csWebHostBuilderExtensions.cs到您的解決方案。

然後您可設置TESTSERVER通過使用這樣的:

var builder = TestServer.CreateBuilder(); 
builder.UseStartup<Startup>() 
    .UseEnvironment("Testing") 
    .UseApplicationPath("YourMvcApplication"); 
var server = new TestServer(builder);