Both are substitutes for an expensive or unavailable component.
Maybe the fake is more dynamic than the mock?
The point seems moot.
Both are substitutes for an expensive or unavailable component.
Maybe the fake is more dynamic than the mock?
The point seems moot.
https://blog.cleancoder.com/uncle-bob/2014/05/14/TheLittleMo...
A mock is a cache for a desired return value (which is hopefully what the real interface would correctly return).
Edit: actually sethammons link is great
But I personally think that instead of spending the time to write a fake, its better to just spend the time writing actual integration test with the real dependency (eg: just run the db in docker or something)
or if you don't want to spend that time, then just record the interaction (eg: db calls) and create "throwaway stubs". Use this stubs as long as they are relevant, and then generate new ones as your code grows. Save time "writing" any mocks, and you don't tend to couple too much with your mocks :P
That creates unit tests that take ages to run - and fail surprisingly in hard to debug ways.
There are scalability limits to this but for most cases it works extremely well and is fast.
Fake is just a simple implementation, like in-memory db to stand-in for a real one. In the optimal case, provided by library authors.
A mock is a set of fixed data; sometimes people even load these from YAML documents and the like. They're often re-used for different tests.
A fake is a "fake" object created when needed, with the parameters you need.
Mock:
user = load_user_mock()
article = load_article_mock()
run_test(user, article)
Fake: user = newUser(Name: "foo", Email: "foo@example.com") # Or generate random data
article = newArticle(User: user, Title: "bar")
run_test(user, article)
The difference is somewhat subtle, but I often find mocks very inconvenient because you can't "just" change one without lots of stuff falling over. It's also much harder to do something "special" with them for that one test.