Mocking JavaScript Requests During Tests

11-Mar-2022 1421
When we run tests, we don’t want to hit external services in most cases, so our tests don’t depend on external services and are more stable. We can use gems like VCR or WebMock to stub the requests that are done by the Rails application, but when the request is initiated by the JavaScript code… that’s a different story.Two Different ApproachesIn both cases we run tests that use the Capybara and Selenium-webdriver gems. This is the default when creating a new Rails application so you should already have them in your Gemfile. The tests can be of type feature, integration, system, etc, because, as long as the type of test uses Selenium and Capybara, we can proxy or intercept the requests.
Use coupon code:

RUBYONRAILS

to get 30% discount on our bundle!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these highly-rated books offer thousands of essential questions and answers to boost your interview success. Buy our 'Ultimate Job Interview Preparation eBook Bundle' featuring 2200+ questions across multiple languages. Ultimate Job Interview Preparation eBook Bundle