I'm connecting to a simple, if idiosyncractic, external service.
I believe that my unit tests should not depend on the availability or implementation of that external service, so I intend to mock it out.
I need the mock to accept and return realistic messages and responses - otherwise my tests won't represent real states of affairs. For example, it has to throw the right kind of errors - and there are at least 7 different ways it can fail (between you and me it's not a very well designed external service). Therefore, at a bare minimum I have to have a hash of message/response pairs.
So instead of reducing contingency, mocking has reintroduced it somewhere else. In fact, as the saying goes, now I've got two problems: I've got to be sure that what's in my hash is a fair representation of how the external service behaves. But surely the canonical source of what response object X gives to message m is X itself. Anything else is risky and messy.
Have I taken a wrong turn? How can I eliminate this apparent circularity?
EDIT I've clarified what I think the problem is in the light of Justice's helpful comments.
What is mocking? Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.
What is mock testing? Mocking means creating a fake version of an external or internal service that can stand in for the real one, helping your tests run more quickly and more reliably.
Refactor your existing code to depend on the new file instead of the service directly. Create a mock implementation of the abstraction layer. You control the inputs and outputs of the mocked function calls. Set up your jest tests to use the mocked version of the service instead of the real one.
Mocks and stubs are very handy for unit tests. They help you to test a functionality or implementation independently, while also allowing unit tests to remain efficient and cheap, as we discussed in our previous post.
Let me refer you to my two answers at another question about unit testing just to avoid repeating myself, first.
The thing I think that mocking gives you in this environment is that it's rigorously specifying what you think the behavior of that external interface is going to be. This means that you've got a controlled test (something tells me this external service changes every so often.) So, not only can you test and debug your code with a known "good" sequence of responses, but you have a documented set of examples of what you expect.
If I were in that situation, and depending on the real service, I'd be tempted to write a unit test or mock for the external service as well. That way, if you observe a fault in real operation, you can (1) run the test against your code using the mock for the external interface, and (2) test the external service against your expectations.
The point, though, is to have something for which you have real confidence and which you completely control.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With