We're trying to test a frontend feature that sends a request to a Third Party API when a button is clicked. The click will trigger an internal API call to our Flask application, which will translate this to a request for the ThirdPartyApi.
We try to test this using Splinter, Flask Testing and PhantomJS. Because we
don't want to hit the actual API, we want to stub out the method that will
perform this request. However, the stub doesn't seem to work and the actual
http call is performed. We suspect it has something to do with the difference
between the test context and the app context, but we were not able to solve it
with the app_context()
provided by Flask.
Does anybody know how to fix this? Below is a simplified version of the test code we are using.
from splinter import Browser
from flask_testing import LiveServerTestCase
from unittest.mock import patch
class ClickButtonTest(LiveServerTestCase):
def test_click_button(self):
browser = Browser('phantomjs')
browser.visit(self.get_server_url() + "/")
with self.app.app_context():
@patch("app.lib.ThirdPartyApi")
class FakeThirdPartyApi:
def some_action(self, param):
return True
browser.find_by_text("Click me!").click()
assert browser.is_text_present("The button is clicked :)")