c# - Setup View engine in ASP.NET MVC6 to work with AspNet.TestHost.TestServer in Unit Tests -


how setup view engine in asp.net mvc 6 work test host created testserver. i've tried implement trick mvc 6 repo:

[fact] public async task callmvc() {     var client = gettesthttpclient();      //call homecontroller.index 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 custom assembly provider. overrides addmvc() because 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; } 

startup class simplest setup mvc:

public class startup {     // method gets called runtime. use method add services container.     public void configureservices(iservicecollection services)     {         // add mvc services services container.         services.addmvc();     }      // configure called after configureservices called.     public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory)     {                // add mvc request pipeline.         app.usemvc(routes =>         {             routes.maproute("default", "{controller=home}/{action=index}/{id?}");         });     } } 

i'm getting response status code not indicate success: 500 (internal server error) , internally it's not able locate index.cshtml view. paths below following unit tests library path or dnx path:

var applicationbasepath = _appenvironment.applicationbasepath; var webrootpath = _env.webrootpath; var basedirectory = appdomain.currentdomain.basedirectory;  

what way setup view engine , environment work unittests using testserver?

your idea of defining different environment going right direction.

i have seen quite elegant solution using extension methods:

https://github.com/bartmax/testservermvchelper

it on nuget can't working there. propably can incorporate 2 classes mvctestapplicationenvironment.cs , webhostbuilderextensions.cs solution.

then can setup testserver using this:

var builder = testserver.createbuilder(); builder.usestartup<startup>()    .useenvironment("testing")    .useapplicationpath("yourmvcapplication"); var server = new testserver(builder); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -