This is a gem vcr
demo app. The app shows current weather of Ho Chi Minh city when you access the root path.
-
You need an API key of WeatherAPI.com. Please create an account and subscribe to this API: https://rapidapi.com/weatherapi/api/weatherapi-com/. Don't worry, it has free plan 😁.
-
Copy API key and set it to
ENV["RAPID_API_KEY"]
. For this app, we only use the Realtime Weather API from WeatherAPI.com
-
Start rails server
-
Access root path.
If you read the code from the HomeController
, you will know that every single time you load the root page, the app call API to get the weather data. In testing, I use vcr
gem to record the API request & response to a cassete file. You will code a real HTTP request for the first request for the recording purpose. After then you can disconnect your internet connection and your test still run smoothly since vcr
replaying the record file.
- First, run the test to make sure everything works fine:
bundle exec rspec
- Then try to disable/disconnect your internet connection and run the test again. It should still pass because
vcr
would replaying the record file. - Go to this file:
spec/requests/home_spec.rb
and:
- Change value of
skip
option totrue
for context "when using gemvcr
" - Change value of
skip
option tofalse
for context "when not using gemvcr
"
- Run the test again. This time the test should fail and raise error like the picture below. This error will occur when you try to have a real HTTP to outside without "notifying"
vcr
. You need to put the code that occur HTTP request in theVCR.use_cassette
block.