I'm trying to stub an API request using Webmock. Instead of getting the "real" data from my Rails controller, I want to return dummy data, for test purposes only.
I have a React frontend with a button that fetches my api endpoint:
const handleClick = async () => {
const response = await fetch("api_endpoint");
const data = await response.json();
console.log("data: ", JSON.stringify(data));
};
This is my test file:
require 'rails_helper'
require 'spec_helper'
RSpec.describe 'visiting the embedded app', type: :system do
it 'visits the embedded app' do
stub_request(:get, 'api_endpoint').to_return(
body: { data: 'dummy' }.to_json
)
visit 'my react page with the button'
click_button "Call API"
sleep 10
random_assert
end
end
Instead of getting data: dummy
, I get the "real" data from by rails controller.
What is wrong with this implementation? Tell me if you need more information!