4 ms·
While I generally don't think tests are bad in general this is a wonderful example of the fundamental problem with software development. Essentially, you have a
by noobermin 3y ago
While I generally don't think tests are bad in general this is a wonderful example of the fundamental problem with software development. Essentially, you have a tool that was supposed to make development easier (tests) eventually change the code to become more complicated to make it easier to use that tool with, which makes development harder.
- skrebbel 3y agoInjecting a Clock or Timer is great not just for automated tests but also for debugging weird behavior. Theres really hardly any downsides.
- bruce343434 3y agoI'm not sure how that would work. Can you give an example?
- EdwardDiego 3y agoHave a look at this method on Java's Instant class for an example. https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#now-java.time.Clock- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.... Code under test creates Instant, so long as it uses the Clock you've passed in, you'll control how it determines when now() is. You've got several built-in testing clocks like this one[1], but can also implement your own. [1]: https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#fixed-java.time.Instant-java.time.ZoneId- https://docs.oracle.com/javase/8/docs/api/java/time/Clock.ht...
- skrebbel 3y agoJust a class (or struct or module or whatever your language has for that) that has a “now()” method which returns the current time. And then replace all your calls that do Date.now() with clock.now() and then you can make “now” be whatever you want.