First, a warning: today’s exercise is likely to generate tasks for you that will take longer than 20 minutes to fix. This doesn’t mean something bad has happened, or that you’ve failed. Just file a ticket to remind you to complete the work later.
Today’s exercise: run your test suite with your internet connection disabled.
Turns out, it’s fairly easy to accidentally rely on an external service during test runs (I’ve done it many times). This won’t just make your test suite slow, but brittle.
Chances are, you’ll see a few cryptic failures when you try this.
In general, I recommend reaching for a tool like Webmock to fix issues like these.
As a bonus, Webmock has a setting to disable external web requests during test runs. If you attempt to make a connection to the outside world, you’ll get an exception. This will prevent you from writing internet-hitting tests in the future.
Used the time allocated to do research on how to mock API’s, never done it. This is definitely something that would be a nice addition to my projects but will take some time to figure out.
However, I assume people don’t mock all api calls because how would you verify the api hasn’t changed and is actually still working as it should?
Do you mock the majority of calls and only leave a few real api calls in your test suite?
On Laravel, so no WebMock. Done this in the past and I know that we have outgoing network requests.
We try to mock them as much as possible, but sometimes (I’m looking at you Azure) it is realy hard to do.
We’re also using VCR, so it’s somewhat guaranteed that no external connection are made during testing… but, to my surprise, our feature specs which are run through selenium failed with a net::ERR_INTERNET_DISCONNECTED… is it not designed to work in airplane mode?
Spent the time today to work on stubbing out interactions with S3 to avoid having to setup it for tests. Quite a long way to go until we are free from external services.
No issues for me on a Laravel project. For anyone looking to up their testing game with Laravel, I’d highly recommend Adam Wathan’s Test Driven Laravel course. I learned a ton from it.
Like virtually all professional Rails apps I worked on, my current codebase uses webmock to disable external requests. But it doesn’t hurt to be paranoiac, so I ran the specs offline and everything was still green.
I have used in past moto and I was amazed how much it can provide.
Regarding mocking requests - I did some experiments in past and it worked well but all my projects are testing against locally running service (which is subject of my tests) so I did not find a need to mock it on regular bases.
About a year ago I have experimented with https://mitmproxy.org/ which amazed me with how much it can do. Apart of allowing me to do “F12” on command line I was also able to mock server responses using it.