TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Best Way to Mock APIs in 2020?

71 点作者 HiddenCanary将近 5 年前
I&#x27;m looking for a good way to Mock APIs for a sideproject, so far I haven&#x27;t found any solution that has blown me away and I&#x27;m wondering If I&#x27;m missing anything.<p>I&#x27;m curious what people recommend for the best way to Mock APIs?

33 条评论

triztian将近 5 年前
I&#x27;ve found mitmproxy (<a href="https:&#x2F;&#x2F;mitmproxy.org" rel="nofollow">https:&#x2F;&#x2F;mitmproxy.org</a>) in reverse mode really useful. I&#x27;m a mobile dev and when the backend work is still pending I usually mock the backend requirements by selectively patching or adding endpoint support, it can be used like so:<p><pre><code> mitmproxy --mode reverse:https:&#x2F;&#x2F;my.privateapi.com -s .&#x2F;path&#x2F;to&#x2F;mockscript.py </code></pre> This allows me to implement most of the UI and server interactions w&#x2F;o being blocked by server work.
评论 #23516899 未加载
peheje将近 5 年前
Can someone explain to me what this is all about? Shouldn&#x27;t you hide all your API calls behind an interface, and then mock that interface? Dependency inversion principle, depend upon abstractions and all that.
评论 #23517297 未加载
评论 #23517423 未加载
评论 #23516237 未加载
评论 #23517215 未加载
评论 #23517460 未加载
评论 #23516724 未加载
评论 #23517019 未加载
robjan将近 5 年前
Depends how advanced the mocking behaviour requirements are. For most projects, I find json-server sufficient.<p>Remember that perfect is the enemy of good enough. If you&#x27;re looking to be blown away by your mock server you may never get round to building your product.
nerdywordy将近 5 年前
<a href="https:&#x2F;&#x2F;miragejs.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;miragejs.com&#x2F;</a> is a great complement to frontend first development. Some folks on my team use it and love it. I tend to dev from back to front so I don&#x27;t run into the need as much. If I do I typically just mock a JSON response in my frontend call function. Depending on the need I&#x27;ll sometimes set the call to return an error response as well.
jcpst将近 5 年前
For a side project? I think that makes a difference here.<p>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.<p>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.<p>That process is usually quick enough that there’s no reason to mock.<p>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 年前
Might not be the best, but the ones I use:<p>For REST API: <a href="https:&#x2F;&#x2F;mockoon.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mockoon.com&#x2F;</a><p>For GraphQL: <a href="https:&#x2F;&#x2F;github.com&#x2F;APIs-guru&#x2F;graphql-faker" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;APIs-guru&#x2F;graphql-faker</a>
kahrensdd将近 5 年前
There are lots of variations depending upon your use case.<p>For unit testing and CI you may want mock objects that are implemented in the same language as your code. Google search for &quot;mock object &lt;language&gt;&quot;. That&#x27;s where you&#x27;ll find Mockito (Java) or Mocha Spy (NodeJS) or Testify (golang). This list never ends.<p>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. <a href="https:&#x2F;&#x2F;docs.cypress.io&#x2F;guides&#x2F;guides&#x2F;stubs-spies-and-clocks.html" rel="nofollow">https:&#x2F;&#x2F;docs.cypress.io&#x2F;guides&#x2F;guides&#x2F;stubs-spies-and-clocks...</a><p>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.). <a href="https:&#x2F;&#x2F;learning.postman.com&#x2F;docs&#x2F;postman&#x2F;mock-servers&#x2F;setting-up-mock&#x2F;" rel="nofollow">https:&#x2F;&#x2F;learning.postman.com&#x2F;docs&#x2F;postman&#x2F;mock-servers&#x2F;setti...</a><p>If you need to capture traffic, check out goreplay or mitmproxy: <a href="https:&#x2F;&#x2F;github.com&#x2F;buger&#x2F;goreplay" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;buger&#x2F;goreplay</a> <a href="https:&#x2F;&#x2F;docs.mitmproxy.org&#x2F;stable&#x2F;" rel="nofollow">https:&#x2F;&#x2F;docs.mitmproxy.org&#x2F;stable&#x2F;</a><p>There is a whole class of &quot;VCR&quot; projects for recording traffic, these tend to be language specific (VCR is in Ruby), but there are ports to other languages: <a href="https:&#x2F;&#x2F;github.com&#x2F;vcr&#x2F;vcr" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vcr&#x2F;vcr</a> <a href="https:&#x2F;&#x2F;github.com&#x2F;bblimke&#x2F;webmock" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bblimke&#x2F;webmock</a><p>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.<p>It&#x27;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 年前
I&#x27;ve used this on multiple projects: <a href="https:&#x2F;&#x2F;github.com&#x2F;typicode&#x2F;json-server" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;typicode&#x2F;json-server</a>
tiborsaas将近 5 年前
I&#x27;ve used this: <a href="https:&#x2F;&#x2F;github.com&#x2F;nock&#x2F;nock" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;nock&#x2F;nock</a> worked great for tests.
XVincentX将近 5 年前
<a href="https:&#x2F;&#x2F;stoplight.io&#x2F;open-source&#x2F;prism" rel="nofollow">https:&#x2F;&#x2F;stoplight.io&#x2F;open-source&#x2F;prism</a>
baggsie将近 5 年前
I&#x27;ve found Hasura to be great for this, maybe slightly more longer to set up but you get a full CRUD graphql setup for little effort.
mpweiher将近 5 年前
Avoid mocking:<p><a href="https:&#x2F;&#x2F;blog.metaobject.com&#x2F;2014&#x2F;05&#x2F;why-i-don-mock.html" rel="nofollow">https:&#x2F;&#x2F;blog.metaobject.com&#x2F;2014&#x2F;05&#x2F;why-i-don-mock.html</a>
m0xte将近 5 年前
<a href="http:&#x2F;&#x2F;wiremock.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;wiremock.org&#x2F;</a> works for me
mdaniel将近 5 年前
I readily admit I haven&#x27;t evaluated every one of the comprehensive URLs in this thread, but back when I was doing integration work between my project and both GitHub and Jira, I found the scripting support in SoauUI (<a href="https:&#x2F;&#x2F;github.com&#x2F;smartbear&#x2F;soapui&#x2F;tree&#x2F;release-5.5.0" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;smartbear&#x2F;soapui&#x2F;tree&#x2F;release-5.5.0</a>) allowed me to model some dynamic behavior, and use the fallback responses when I didn&#x27;t care about specifying every single one of them<p>Despite its name, SoapUI has strong support for REST, and with a little massaging maybe even GraphQL also
xgenecloud将近 5 年前
If you have a database - try this <a href="https:&#x2F;&#x2F;github.com&#x2F;xgenecloud&#x2F;xgenecloud" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;xgenecloud&#x2F;xgenecloud</a><p>Automatically generates REST &amp; GraphQL APIs within seconds from your database schema.<p>Here is a demo on how to create 8000+ REST APIs within 10 seconds - <a href="https:&#x2F;&#x2F;youtu.be&#x2F;NtCwnlLudnk" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;NtCwnlLudnk</a><p>Not just mock - you may even consider writing the full backend on it.<p>Im the creator - happy to answer any questions.<p>edit : added demo link
disposedtrolley将近 5 年前
If the API provider supplies an OpenAPI definition, you can give API Sprout a try. It’ll parse the definition and spin up a mock server returning the example data you want. You can also modify the OpenAPI definition manually if you need different data to be returned.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;danielgtaylor&#x2F;apisprout" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;danielgtaylor&#x2F;apisprout</a>
jacobr将近 5 年前
Haven’t tried it yet, but <a href="https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;msw" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;msw</a> looks very interesting. It uses Service Workers in the browser to intercept requests to the APIs, instead of running a real mock server.<p>There’s support for mocking in Node as well for SSR and jsdom tests.
pinacarlos90将近 5 年前
Use swagger-openAPI3 and generate a spec. You can then use the spec to spin up clients or import to a API management tool and produce mock data, with support for authentication and more. I recommend using swashbuckle (for .netCore) if you already have the code for your API, it allows you to quickly produce spec from code.
coold将近 5 年前
I&#x27;m using Wiremock
thiht将近 5 年前
Shameless plug: I developed Smocker[1] for exactly this use case. It&#x27;s not the only solution but we have a few features not present anywhere else that matched exactly our use case.<p>Hope it can help you :)<p>[1]: <a href="https:&#x2F;&#x2F;smocker.dev" rel="nofollow">https:&#x2F;&#x2F;smocker.dev</a>
mcintyre1994将近 5 年前
I’ve really liked working with mmock <a href="https:&#x2F;&#x2F;github.com&#x2F;jmartin82&#x2F;mmock" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jmartin82&#x2F;mmock</a><p>It has a nice API for inspecting the requests made to the mock server and a nice web UI for debugging too.
lbroudoux将近 5 年前
I would suggest having a look at Microcks (<a href="https:&#x2F;&#x2F;microcks.io" rel="nofollow">https:&#x2F;&#x2F;microcks.io</a>) as a way to produce mocks from OpenAPI contracts, Postman collection or other assets...<p>Full disclosure : I am the founder of the project ;-)
评论 #23521590 未加载
评论 #23517024 未加载
tomakehurst将近 5 年前
MockLab [1] is a SaaS mocking tool built on top of WireMock.<p>Worth a look if you use Swagger&#x2F;OpenAPI as it&#x27;ll accept this as an import format.<p>[1]: <a href="http:&#x2F;&#x2F;mocklab.io" rel="nofollow">http:&#x2F;&#x2F;mocklab.io</a>
davegb3将近 5 年前
If you like wiremock, try <a href="https:&#x2F;&#x2F;intervene.dev" rel="nofollow">https:&#x2F;&#x2F;intervene.dev</a> - config in TypeScript with automatic https and CORS.<p>Open for questions + feedback [author]
isuckatcoding将近 5 年前
Don’t bother with mock APIs tbh. I assume you’re trying to unit test the front end?<p>If anytime, write integration tests using something like selenium or whatever the latest hotness is.
sidcool将近 5 年前
I use Mountebank extensively and it&#x27;s a pleasure to use.
htyden将近 5 年前
<a href="https:&#x2F;&#x2F;intervene.dev&#x2F;" rel="nofollow">https:&#x2F;&#x2F;intervene.dev&#x2F;</a> has been very useful for me.
poke111将近 5 年前
<a href="https:&#x2F;&#x2F;www.mock-server.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.mock-server.com&#x2F;</a>
hckr_news将近 5 年前
Nock and JSON server are useful libraries
lovetocode将近 5 年前
A lot of Ruby’s test frameworks have this functionality built in. What technology are you using
mister_hn将近 5 年前
Api-framework works really good for this
vkku将近 5 年前
Mockito exists as an option
评论 #23519608 未加载
nmasse-itix将近 5 年前
I was wondering if you had a chance to have a look at Microcks (<a href="https:&#x2F;&#x2F;microcks.io&#x2F;" rel="nofollow">https:&#x2F;&#x2F;microcks.io&#x2F;</a>) ?
评论 #23516920 未加载