requests-mock/docs/fixture.rst
Jamie Lennox 833ee180c3 Create the Mocker
A standard means of mock loading the adapter. Because we have this move
fixtures stuff into contrib.

Add some documentation for all this.
2014-06-23 16:03:40 +10:00

1.3 KiB

Fixtures

Fixtures provide a way to create reusable state and helper methods in test cases.

To use the requests-mock fixture your tests need to have a dependency on the fixtures library and the mock library. These are not provided by requests-mock.

Overview

The fixture mocks the :pyrequests.Session.get_adapter method so that all requests will be served by the mock adapter.

The fixture provides the same interfaces as the adapter.

>>> import requests
>>> from requests_mock.contrib import fixture
>>> import testtools

>>> class MyTestCase(testtools.TestCase):

...     TEST_URL = 'http://www.google.com'

...     def setUp(self):
...         super(MyTestCase, self).setUp()
...         self.requests_mock = self.useFixture(requests_mock.Mock())
...         self.requests_mock.register_uri('GET', self.TEST_URL, text='respA')
...
...     def test_method(self):
...         self.requests_mock.register_uri('POST', self.TEST_URL, text='respB')
...         resp = requests.get(self.TEST_URL)
...         self.assertEqual('respA', resp.text)
...         self.assertEqual(self.TEST_URL, self.requests_mock.last_request.url)
...