Every story and comment from the Hacker News API, in a layout borrowed from 2001.
A comment on Why a mock doesn’t work. Parent comment
Dependency injection can be as simple as:
class Printer: def run(self): print("hello world") class Caller: def __init__(self, printer_class=Printer): self.printer = printer_class() def doit(self): self.printer.run()
If the point is just having simpler testability and making dependencies more explicit, this gets the job done.
And since Python is a dynamic language, you can use this with stdlib classes too, so it solves your first complaint.
I've seen it a lot in Ruby and C# lately.
There are no comments on this comment yet.
Dependency injection can be as simple as:
In your tests all you have to do is pass a different object to your Caller class. No need to mess with factories, interfaces or Impl suffixes.If the point is just having simpler testability and making dependencies more explicit, this gets the job done.
And since Python is a dynamic language, you can use this with stdlib classes too, so it solves your first complaint.
I've seen it a lot in Ruby and C# lately.