jflaga
jflaga

Reputation: 4779

An exception of type 'System.TypeInitializationException' occurred in System.Web.Mvc.dll

I created a Unit Test project using Visual Studio 2013 Express for Web.

and my test is this

    [TestMethod]
    public void it_should_return_the_home_view()
    {
        // arrange
        HomeController controller = new HomeController();

        // act
        ViewResult result = controller.Index() as ViewResult;

        // assert
        Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
    }

My HomeController has this action method:

    public ActionResult Index()
    {
        ViewBag.Message = "Welcome!";
        return View();
    }

When I run the test above an exception occurs at "return View()"

The exception message is "An exception of type 'System.TypeInitializationException' occurred in System.Web.Mvc.dll"

Upvotes: 1

Views: 3292

Answers (1)

malibeg
malibeg

Reputation: 2267

Had same error. So I referenced this assembly in my unit test project System.Web.WebPages.dll.

If you create MVC project with VS default template and check to add unit test project all necessary references are included.

For empty unit test projects targeting MVC 5 project best solution is to add mvc5 via nuget as suggested here

Upvotes: 2

Related Questions