I want to test a method with following signature.
int SomeMethod(List<Employee> employees)
Here are the related classes
public class Employee
{
public int CustomerID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string StreetName { get; set; }
public string CityName { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
}
How can I mock the List<Employee> as an input to SomeMethod? Note that Employee and Address classes has no interface or virtual method.
If you want to test method with following signature int SomeMethod(List<Employee> employees) you don't need to mock Employee!
You need to create List<Employee> employees = new List<Employee>(), pass to method and verify result!
Employee and Address are models without any functionalities and you don't need to mock them!
Here are two thoughts about your case:
You can legally call new Employee() or new Address() in your method because that code is testable! Creating new instances of models doesn't execute external dependency.
It will be problem to call new Employee() or new Address() only if they have functionalities. In that case you will execute real dependency which is maybe not testable! Example, if Employee or Address communicate with database it is not testable because it will connect to real database when test is executed. Than you need to create mock to avoid database connection.
Without an interface or common base class between Employee and your mock object you cannot pass any object except for Employee.
If you have the ability to, I would suggest creating an Interface for your employee class & mock class to both implement. Then you would just change your method parameter to accept the interface instead of Employee directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With