DEV Community

Cover image for How to stop API calls in RSpec and Return your Own Data

How to stop API calls in RSpec and Return your Own Data

Robert Hustead on October 27, 2018

tl;dr You can explicitly return whatever data you want from external API requests using Webmock and stubbing requests. How do you test a Controlle...
Collapse
 
epigene profile image
Augusts Bautra

A quick note, often you'll want to return status 200 with a specific body. Since status 200 is to_returns default, you can simply write to_return(body: "something").

Collapse
 
husteadrobert profile image
Robert Hustead

Wow, thanks for the info! The less code you write, the less chance for bugs and errors!

Collapse
 
juliusdelta profile image
JD Gonzales • Edited

Good advice. On smaller projects I tend to do something similar but on my work production application we use the VCR gem which records one call to a yml (a cassette) and then every time the test runs it uses that yml in place of the api response.