This test is to check that I can return a ViewModel object by creating a customer and calling the Details() controller method.
[TestMethod()]
public void Can_View_AccountDetails()
{
AccountController target = new AccountController(null, null, null, null);
target.customer = new Customer { Id = 4, Active = true, BillingAddress_Id=1, ShippingAddress_Id=2 };
// Act
ActionResult result = target.Details();
// Assert
var resultData = ((ViewResult)result).ViewData.Model as AccountViewModel;
Assert.IsInstanceOfType(resultData, (typeof(AccountViewModel)));
}
‘customer’ is a member of the controller base class, which is then assigned in Initialize(). Initially I couldn’t assign anything to it, but by setting it to ‘public’ rather than ‘protected’ I was able to use it in my test and avoided trying to call the base class Initialize() method.
EDIT: ‘customer’ is populate from a Repository object injected into the base class constructor.
Is this the right way to do this? It seems somehow wrong to change the accessibility level in order to get the test to work.
Also, although I’m trying to use Moq to create my tests, I’m not actually doing any mocking at all here which again, doesn’t seem right.
I think your real problem is that the customer information “magically” shows up within the
AccountController. TheCustomerinstance should be injected into theAccountControllerfrom the outside since it is an external dependency. That being the case you would not have to make thecustomerproperty public because you pass it into theAccountControlleryourself.