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