Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

His recommended solution prevents parallelizing your unit tests. Dependency injection is not just testable globals. It's about declaratively defining what global-ish things your class depends on.

You also don't need to provide the date as a parameter to your methods. You can make the class depend on a clock. A clock is a natural thing for a service to depend on, and clearly indicates to outside users that the class needs to know about time.

If it isn't a service, and you're updating some sort of entity, then why should the entity figure out the current time itself? The model should just record that I updated such and such field at such and such time. If it's the current time, that's someone else's responsibility.



> His recommended solution prevents parallelizing your unit tests.

How so?


Because monkeypatching is global. It shouldn't require more than a moment of thought to realize this...


Eh, Ruby doesn't do well with parallelism anyway. If you're running your tests in parallel, you're usually running them in different processes. Or on different machines entirely. Either way, monkey patching one of those processes won't affect other ones.

Besides, if there is a testing library that runs multiple tests in parallel in the same processes (which maybe there is and I'm just not aware of it), making your mocking library smart enough to handle that, isn't difficult. Stubbing only for a particular thread, for instance.


Yeah, ruby doesn't do well with parallelism, and if you want parallel tests, you're probably doing it in separate processes, because the ruby community chooses to completely ignore everything that's known about how to write code that behaves well in parallel because "eh, I don't do it, so it must not be important".


Because Time.now is a global method whose behavior you've just (temporarily) modified. Any tests or code that do depend on Time.now returning the actual current time won't work correctly.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: