0

I am displaying chart in mvc.

 public class HomeController : Controller
    {
        MapContext context = new MapContext();

        public ActionResult Index()
        {

        }
    }

How do i write test case to check data. I am fairly new to MVC

Richa
  • 3,261
  • 2
  • 27
  • 51
  • Do you have much experience of TDD? It may be worthwhile going back and learning a bit about it from a more abstract point. You will need to setup a test project, mock certain aspects of your application etc. – nik0lai May 12 '15 at 09:01

2 Answers2

2

You'll find it hard to write a true unit test because you're instantiating a concrete instance of MapContext. Ideally you'd use IoC/Dependancy Injection to inject your MapContext and then you'd really want to create an Interface for it so it can be faked / mocked otherwise you're not just testing the controller you'retesting the MapContext then it is no longer a unit test but an intergration test..... lost yet?!

Controller would look something like:

public class HomeController : Controller
{
    IMapContext _context;

    public HomeController(IMapContext mapContext)
    {
        _context = mapContext;            
    }

    public ActionResult Index()
    {
        var x = (from c in _context.Area
                     select c.Name).ToArray();

       var y = (from c in _context.Area
                     select c.Pin).ToArray();

        var bytes = new Chart(width:500, height: 300)
        .AddSeries(
         chartType: "Column",
         xValue: x,
         yValues: y)
        .GetBytes("png");
        return File(bytes, "image/png");
    }
}

Then your unit test would be something like (Nunit + Moq) :

[TestFixture]
public class HomeControllerTest
{
    Mock<IMapContext> _mapContext;

    [SetUp]
    public void SetUp()
    {
        _mapContext = new Mock<IMapContext>();
    }

    [Test]
    public void BasicTest()
    {
        HttpConfiguration configuration = new HttpConfiguration();
        HttpRequestMessage request = new HttpRequestMessage();

        var homeController = new HomeController(_mapContext.Object);
        homeController.Request = request;

        var result = homeController.Index();

        Assert.IsNotNull(result);
        Assert.AreEqual(<somevalue>, result.SomeProperty);
     }
}

Obviously you'll need to put in a proper value in to test against and change SomeProperty to a real property.

Further reading would be to find out more about

  • Ioc/Dependancy Inject
  • Nunit
  • Moq

EDIT

Here are some tutorials that should help you get started

http://www.asp.net/mvc/overview/older-versions-1/unit-testing/creating-unit-tests-for-asp-net-mvc-applications-cs

https://msdn.microsoft.com/en-GB/library/dd410597(v=vs.100).aspx

https://msdn.microsoft.com/en-us/library/ff847525(v=vs.100).aspx

matt_lethargic
  • 2,706
  • 1
  • 18
  • 33
  • I tried your method. But it still does not seem to work – Richa May 12 '15 at 12:58
  • What doesn't work? Saying it doesn't work doesn't help! You need to provide more information. My method does work as I do it on a daily basis for my job. You asked for someone to show you how to write a test and I did. Everyone here has suggested you do some more reading around TDD, Unit Testing MVC etc did you do that? While it's really good that you are trying to get into testing, if you're new to C# and MVC then you'll have to do a lot more learning. I've edited my answer and added links to tutorials that might help you get started with unit testing MVC – matt_lethargic May 12 '15 at 13:08
0

You can the following steps to get started with writing unit test cases for yoyr action methods

Creating Unit Tests for ASP.NET MVC Applications

MVC Unit Testing Unleashed

How should one unit test a .NET MVC controller?

Community
  • 1
  • 1
Amal Dev
  • 1,938
  • 1
  • 14
  • 26