0

I am having a little bit of trouble getting unit tests to run for code implemented in an ASP.NET application.

This is an issue when trying to create a new unit test project in Visual Studio 2012. In 2010, I remember that unit tests designed for ASP.NET were generated automatically. In other words, before running the unit tests, the unit test project would open an instance of the website and run the tests through it. This is no longer the case it seems.

To create the unit test project, I go to New Project->Unit Test Project. It used to be that I would right click on a function and I would get Create Unit Tests... which would create a unit test project designed for ASP.NET applications.

When I create the normal unit test project however, the code crashes on ASP.NET specific functionality. In this case I have a crash on HttpContext.Current.Trace.Write with a NullRefereceException

Does anyone know how to resolve this issue?

Serguei Fedorov
  • 6,947
  • 9
  • 55
  • 86
  • Check out the answers here: http://stackoverflow.com/questions/9624242/setting-httpcontext-current-session-in-a-unit-test – John Koerner Jan 13 '15 at 17:31
  • Hmmm. This might work but seems like I will have to alter the code base of the main project to get it to work. A lot of what is crashing is wrapped up in DLLs and I would much prefer to leave those alone. – Serguei Fedorov Jan 13 '15 at 17:34
  • 1
    The second answer might allow you to set the context properly without modifying the underlying code you are testing. – John Koerner Jan 13 '15 at 17:35
  • http://stackoverflow.com/a/19699264/1260028 this answer worked really well. Thank you for pointing me in the right direction! – Serguei Fedorov Jan 13 '15 at 17:42

0 Answers0