I am attempting to test the Index
action of a controller. The action uses AutoMapper to map a domain Customer
object to a view model TestCustomerForm
. While this works I am concerned about the best way to test the results that I am receiving from the Index
action.
The controller's index action looks like this:
public ActionResult Index()
{
TestCustomerForm cust = Mapper.Map<Customer,
TestCustomerForm>(_repository.GetCustomerByLogin(CurrentUserLoginName));
return View(cust);
}
And its TestMethod
looks like this:
[TestMethod]
public void IndexShouldReturnCustomerWithMachines()
{
// arrange
var customer = SetupCustomerForRepository(); // gets a boiler plate customer
var testController = CreateTestController();
// act
ViewResult result = testController.Index() as ViewResult;
// assert
Assert.AreEqual(customer.MachineList.Count(),
(result.ViewData.Model as TestCustomerForm).MachineList.Count());
}
In the CreateTestController
method I use Rhino.Mocks
to mock a customer repository and set it up to return the customer from SetupCustomerForRepository
. In this manner I know that the repository will return the intended customer when the Index
action calls _repository.GetCustomerByLogin(CurrentUserLoginName)
. Therefore, I figure asserting an equal count is adequate to satisfy IndexShouldReturnCustomerWithMachines
.
All of that said I am concerned as to what I should be testing.
- It seems presumptuous to cast the
result.ViewData.Model as TestCustomerForm
. Is this really an issue? This concerns me because in this instance I am not truly doing test driven development and it seems like I am counting on a particular implementation to satisfy the test. - Are there more appropriate tests to ensure correct mapping?
- Should I be testing each mapped property from the
TestCustomerForm
? - Are there more general controller action tests that I should be doing?