Given the following Javascript, would it be better/more idiomatic to inject the MyService object into myMethod so that a fake version of MyService could be injected for testing? Or am I missing something?
var myObject = {
myMethod: function() {
var myService = new MyService();
return myService.doSomething();
}
}
Dependency injection always takes precedence over hard-coding the dependencies. Also, by coding to interfaces (assuming the myObject instance has been given a copy of a Service object that has a
doSomething()method) could come handy here.