Ask HN: Best Way to Mock APIs in 2020?
71 points| HiddenCanary | 5 years ago | reply
I'm curious what people recommend for the best way to Mock APIs?
71 points| HiddenCanary | 5 years ago | reply
I'm curious what people recommend for the best way to Mock APIs?
[+] [-] triztian|5 years ago|reply
[+] [-] e12e|5 years ago|reply
I think we can use both: https://docs.mitmproxy.org/stable/addons-scripting/
For mocking, and dump/replay for debugging for some of our projects.
https://docs.mitmproxy.org/stable/tute-clientreplay/
[+] [-] peheje|5 years ago|reply
[+] [-] pydry|5 years ago|reply
1) Join a project and fume about how they didn't write nice, clean, "testable" code.
2) Build some sort of integration testing framework that also mocks out REST API calls relatively cheaply (e.g. using recording/playback against a live system).
3) Realize that I can now refactor the code such that it is nice, clean and more easily "unit testable".
4) Realize that with an effective and easy to use, fast integration testing framework the lack of dependency inversion doesn't bother me nearly as much as it used to.
5) Wonder if dependency inversion is actually more of a hack to deal with the sheer utter crappiness of unit tests... and whether making vast architectural changes to code in order to accommodate the sheer ineffectiveness of the current state of test tooling is really the height of wisdom after all.
In a number of these projects there were noises about rewriting the whole damn thing in a different language (usually because management wanted to consolidate hiring around fewer languages), and it occurred to me that it would actually be kind of great to write tests where the entire implementation including the language itself could be swapped out without really changing the tests.
[+] [-] anonymoushn|5 years ago|reply
- Now it's harder to navigate my codebase because I introduced an extra layer of indirection everywhere only for the sake of testing.
- If there is some difference between how the actual protocol client and actual upstream behave and how my fake interface behaves in test, my tests will be wrong, and I will find out when I push to prod. If my tests exercised the actual code that speaks the actual protocol to a fake upstream, I may have caught the issue in the tests.
- If I ever want to replace the process entirely with a completely different one, I must now throw away my tests. If my tests tested the end-to-end behavior of the system, I would be able to keep my tests.
[+] [-] emilsedgh|5 years ago|reply
If the only reason for abstraction is testing, then mocking an interface seems like a more valid choice to me.
[+] [-] djohnston|5 years ago|reply
How is that different then defining an API and then having stub methods on the front returning pretend data?
[+] [-] bspammer|5 years ago|reply
[+] [-] BiteCode_dev|5 years ago|reply
[+] [-] HiddenCanary|5 years ago|reply
[+] [-] robjan|5 years ago|reply
Remember that perfect is the enemy of good enough. If you're looking to be blown away by your mock server you may never get round to building your product.
[+] [-] nerdywordy|5 years ago|reply
[+] [-] jcpst|5 years ago|reply
What I’ve been doing lately, in my own time outside of work, is setting up a postgres DB, pointing postgraphile to it, then I have the API out of the way.
From there, if I’m consuming that API in a statically-typed language, I’ll look at generating the models I need from the graphql schema with quicktype.
That process is usually quick enough that there’s no reason to mock.
I suppose if I was doing UI first, I would just have the repository layer that’s abstracting my calls to whatever just have a function that returns a dumb static object in the shape I expect.
[+] [-] zoontek|5 years ago|reply
For REST API: https://mockoon.com/
For GraphQL: https://github.com/APIs-guru/graphql-faker
[+] [-] kahrensdd|5 years ago|reply
For unit testing and CI you may want mock objects that are implemented in the same language as your code. Google search for "mock object <language>". That's where you'll find Mockito (Java) or Mocha Spy (NodeJS) or Testify (golang). This list never ends.
Specifically for unit testing of a UI, you may want your browser driver to handle this, ex: Cypress has built-in support for mock AJAX endpoints. https://docs.cypress.io/guides/guides/stubs-spies-and-clocks...
If you want an endpoint you can call, Postman has a feature for this, there are several others like this in the comments (JSON Server, mmock, mountebank, etc.). https://learning.postman.com/docs/postman/mock-servers/setti...
If you need to capture traffic, check out goreplay or mitmproxy: https://github.com/buger/goreplay https://docs.mitmproxy.org/stable/
There is a whole class of "VCR" projects for recording traffic, these tend to be language specific (VCR is in Ruby), but there are ports to other languages: https://github.com/vcr/vcr https://github.com/bblimke/webmock
The vendor products tend to be labelled Service Virtualization. I used to work for one of those companies, ITKO, we were acquired by CA Technologies (now Broadcom) in 2011. There are vendor products from Micro Focus, Tricentis, Broadcom, Parasoft, etc.
It's important to think about your use case: local development, unit testing, CI, integration testing, performance testing, recording vs. programming, protocol support, payload support, etc. Many of the tools focus on just a subset of these areas.
[+] [-] forresponse|5 years ago|reply
[+] [-] tiborsaas|5 years ago|reply
[+] [-] XVincentX|5 years ago|reply
[+] [-] baggsie|5 years ago|reply
[+] [-] mpweiher|5 years ago|reply
https://blog.metaobject.com/2014/05/why-i-don-mock.html
[+] [-] m0xte|5 years ago|reply
[+] [-] mdaniel|5 years ago|reply
Despite its name, SoapUI has strong support for REST, and with a little massaging maybe even GraphQL also
[+] [-] xgenecloud|5 years ago|reply
Automatically generates REST & GraphQL APIs within seconds from your database schema.
Here is a demo on how to create 8000+ REST APIs within 10 seconds - https://youtu.be/NtCwnlLudnk
Not just mock - you may even consider writing the full backend on it.
Im the creator - happy to answer any questions.
edit : added demo link
[+] [-] disposedtrolley|5 years ago|reply
https://github.com/danielgtaylor/apisprout
[+] [-] jacobr|5 years ago|reply
There’s support for mocking in Node as well for SSR and jsdom tests.
[+] [-] pinacarlos90|5 years ago|reply
[+] [-] coold|5 years ago|reply
[+] [-] thiht|5 years ago|reply
Hope it can help you :)
[1]: https://smocker.dev