4 ms·
I'd like a language that makes it easy to mock and dependency inject without doing anything special. IE, if in my business logic, I write "new Foo()", I'd like
by gwbas1c 5y ago
I'd like a language that makes it easy to mock and dependency inject without doing anything special.
IE, if in my business logic, I write "new Foo()", I'd like to be able to write a unit test where I say something like, "whenever I wrote 'new Foo()' swap in this mock object instead."
Or, in a module that's an entry-point, I'd like to say, "whenever I wrote 'new Foo()' swap in this subclass instead."
I've spent so much time refactoring to make code mocking and dependency-injectable.
- divs1210 5y agoClojure does something very much like this using the with-redefs [0] form. If you have a function like: (defn send-mail [receiver-ids title text] ...) You can mock it out in your tests like: (with-redefs [send-mail (fn [r-ids title text] (println r-ids title text))] ;; send-mail will just print ...) [0] https://clojuredocs.org/clojure.core/with-redefs https://clojuredocs.org/clojure.core/with-redefs
- riffraff 5y agoTake a look at newspeak (smalltalk-ish, it avoids global references enforcing each component to receive dependencies as arguments, i.e. it has DI builtin) and NesC (C with components, which are composed at build time). Neither is popular but they're pretty interesting!
- jnwatson 5y agoPython already has everything you need. The most important aspect is that you don't have to change your code to use it. https://docs.python.org/3/library/unittest.mock.html https://docs.python.org/3/library/unittest.mock.html