Mocking Form Fields
The MockHandlerMixin class found in q2_sdk.tools.testing.mock_handler_mixin provides two functions for setting
the form fields a route sees in self.form_fields. As with Mocking Wedge Address Configs, the mock class in the test file
needs to inherit this mixin class.
class MockTestingTestHandler(extension.TestingTestHandler, MockHandlerMixin):
Plain routes
mock_form_fields sets self.form_fields to the dictionary it is given.
async def test_submit():
handler = MockTestingTestHandler()
handler.mock_form_fields({"first_name": "Bart"})
await handler.submit()
Routes using @ajax
The frontend sends the body of an ajax request as a single base64-encoded data form field, and the
@ajax decorator decodes it into self.form_fields before the route runs. Fields set with
mock_form_fields are discarded by the decorator, so use mock_ajax_form_fields instead. It encodes the dictionary
the same way the frontend does.
async def test_get_account():
handler = MockTestingTestHandler()
handler.mock_ajax_form_fields({"account_id": 42})
response = await handler.get_account()