News for hackers. Stuff that matters.

mralston

Re:Testable IO in Haskell on Sunday June 21, 2015 @02:15PM (#9754380)
Haskell lets us have generic "mocks", which is -way- better than any kind of expectation-based mock. (admittedly, the example in andy's blog post doesn't show the power of this technique very well)

But my experience with mocks is that expectation-based mocks are fragile and make it easy to write tests that pass but are fundamentally wrong; if you just say you expect XYZ to come out of the database, even if your code gets refactored to not insert XYZ into the database earlier, the test will pass despite the code being completely broken.

With the haskell World approach, you don't have that - the fake database is fake because it's in memory (and thus very fast and uses a blank state for each test), but it's actually implementing the same semantics as the real database.

Some of the other IO stuff we've encapsulated is just expectation-based mocks and as you said that's not new, but it's still clean and doesn't require any changes to the code under test to make it mockable, which we did when we used mocks in our PHP.

Re:Testable IO in Haskell on Sunday June 21, 2015 @01:33PM (#9754196)
The thing is that while the implementation of the fake can get ugly, that's effectively library code that's written once - the actual application code is incredibly clean since all you have to do to it is tag it as being World instead of IO.

Likewise, for tests, the actual interface in practice is clean - we have a few fake-World-only setup functions, and when testing things like the database we can just use the regular insert interface to do our setup.