← Back to context

Comment by realusername

5 years ago

> Why is it important for the sake of testing to be able to alter the runtime behaviour externally without changing the code?

Because that's additional cruft that you don't want when reading your code. Yeah I get it, you can pass dozen of abstract classes to each constructor for each thing you are mocking (or equivalent if it's not a class-based language) that you replace if needed, the only problem with that is that it's inconvenient, prone to mistakes (you can forget some) and makes the code ugly (you don't want to read testing code on the main code).

I've done it in multiple static languages and at best it's a workaround that you should not have to deal with. I should add that any additional barrier to writing tests like this one also reduces the likelyhood that your app is well tested since some developers of your team might not bother going as far as that.

> Spies are possible with e.g. the ReaderT pattern.

After looking online, I'm not sure how that works, that does not looks very convenient for sure. Is that possible with that to say something like "the method X of class Y was executed 2 times with the parameters Z" without changing your code?

> I don't know what this means. I looked at several articles describing some kind of factory pattern in TDD — all of which were horrifically verbose — and all I can glean from that is we are talking about mocking some function which generates objects.

Something like https://github.com/thoughtbot/factory_bot

> This is no different from mocking other system boundaries, which I have already addressed.

Changing the time should also affect any sub dependency of your code which is calling to get the current time, not only your code.