unit-testingasp.net-mvc-controller

MVC Controller unit tests writing


I have a question related to unit tests. I have MVC controller in which i have different actions and i want to write unit tests on all actions.I have written unit tests on classes/functions using NUnit and VS Unit Test framework but Can you recommend any framework to write unit tests on controller actions?


Solution

  • Referencing Creating Unit Tests for ASP.NET MVC Applications (C#)

    From what you have already stated you should be able to use the tools that you already have NUnit and VS Unit Test framework

    For example

    [Test]
    public void TestDetailsViewData()
    {
        var controller = new ProductController();
        var result = controller.Details(2) as ViewResult;
        var product = (Product) result.ViewData.Model;
        Assert.AreEqual("Laptop", product.Name);
    }