MVC and Unit Testing
May 4, 2010 Leave a comment
I set up a entity framework in my model for Northwind:
I then created a Region Controller to return all of the regions for the Index() Method
public ViewResult Index()
{
var region = (from r in dataContext.Regions
select r);
return View(region.ToList());
}
I then set up a Unit Test to check to make sure I am getting 4 regions back (Yes, I know I should be using a stub).
[TestMethod()]
public void IndexTest()
{
RegionController target = new RegionController();
List<Region> regions = (List<Region>)target.Index().ViewData.Model;
int expected = 4;
int actual = regions.Count;
Assert.AreEqual(expected, actual);
}
The thing that surprised me is that the chain I needed to go though to get to the model and the cast. However, seeing this:
makes it all worthwhile.