I was writing a test, and I had to pass a mock into a particular method. I was wondering, is there any benefit of passing it via the constructor or directly into the method in question. Or does it not really matter.
For example
Passing interface/mock via the constructor
class User()
{
IClock clock;
User(IClock clock) {this.clock = clock;}
User GetUser(){ ..}
UpdateUser(User user) {
...
this.clock.Now();
...
}
}
vs
Passing interface/mock into the method
class User()
{
User GetUser(){ ..}
UpdateUser(IClock clock, User user) {
...
clock.Now();
...
}
}
Thanks!
EDIT
In this case, IClock will be wrapping DateTime. I do this for testability. So effectively, I will have an override that will construct the IClock within itself.
EG. For the method case:
UpdateUser(User user) {
UpdateUser(new Clock(), user);
}
Clock will encapsulate DateTime.
IClock in this case is a hidden singleton – it produces the current date-time from the underlying operating system. If you want to write reliable acceptance tests you will need to stub out the date-time provider in those tests.
Because of this you will need to expose this dependency all the way up to the module boundary, and then inject a common date-time source into your module (and perhaps even the whole system). You will sometimes stub this “IClock” date time source when writing your acceptance tests.
This will allow you to answer questions such as:
Having the IClock dependency in either the constructor signature or the method signature will clearly document to the user that your method or object is date-time dependent. Dependence on date-time is normally of critical importance when trying to understand the behaviour of an object so should be given prominence. Typically avoid anything that hides this dependency (such as supplying a default date-time source in an overloaded method definition) as this will prevent you from writing acceptance tests like those that I have described above.
Choosing between passing a dependency into the constructor or the method will typically depend on the responsibilities of the method to the rest of the system. The methods form the public protocol that the object will use to communicate with its peers in the running system, whereas the constructor parameters are the dependencies that this particular implementation needs to perform its role.
Ask yourself the questions: