Make testing style more consistent (#168)
Several overall style changes:
1. Avoid plain `Mock()` and `Mock(spec=thing)`., prefer `mock.create_autospec()`.
1. Don't `mock.patch` without `autospec`.
1. Don't give mock instances special names. Prefer `thing` over `thing_mock` and `mock_thing`.
1. When using `mock.patch`, use the same name as the item being patched to refer to the mock.
```python
with mock.patch('module.thing') as thing:
...
```
and
```
@mock.patch('module.thing')
def test(thing):
...
```
1. Test helper factories should follow the naming convention `make_thing()`.
1. Use `ThingStub` when creating semi-functioning subclasses for testing purposes.
diff --git a/tests/test_iam.py b/tests/test_iam.py
index 97fa907..cc09085 100644
--- a/tests/test_iam.py
+++ b/tests/test_iam.py
@@ -28,13 +28,15 @@
def make_request(status, data=None):
- response = mock.Mock(spec=transport.Response)
+ response = mock.create_autospec(transport.Response, instance=True)
response.status = status
if data is not None:
response.data = json.dumps(data).encode('utf-8')
- return mock.Mock(return_value=response, spec=transport.Request)
+ request = mock.create_autospec(transport.Request)
+ request.return_value = response
+ return request
def make_credentials():
@@ -54,7 +56,8 @@
class TestSigner(object):
def test_constructor(self):
request = mock.sentinel.request
- credentials = mock.Mock(spec=google.auth.credentials.Credentials)
+ credentials = mock.create_autospec(
+ google.auth.credentials.Credentials, instance=True)
signer = iam.Signer(
request, credentials, mock.sentinel.service_account_email)