After we added sessions in our asp web app, now our controller unit tests fail:
data_browser.Tests.HomeControllerTests.Index [FAIL]
System.NullReferenceException : Object reference not set to an instance of an object
Tests fail on a statement where we use sessions:
HttpContext.Session.SetString("games", JsonConvert.SerializeObject(games));
Looks like as a session service needs to be added. In our app it is done through Startup class's methods:
public void ConfigureServices(IServiceCollection services)
{
// Add MVC services to the services container.
services.AddMvc();
services.AddSession();
services.AddCaching();
}
and
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseSession();
...
}
But when we unit test, we just instantiate our controller smth like this:
HomeController homeCtrler = new HomeController();
JsonResult jsonResult = (JsonResult)homeCtrler.Smth();
Assert.Eq(bla, bla);
So is there a way to inject sessions for controller in asp.net 5 unit tests?