Reputation: 14052
I am trying to mock the datastore Client
interaction in Python.
Thus I patched Query
:
from google.cloud.datastore import Client
from unittest.mock import patch
def foo():
client = Client()
try:
ancestor = client.key("anc", "123")
query = client.query(kind="child", ancestor=ancestor)
for q in query.fetch(limit=1):
assert False, "Successfully mocked everything"
finally:
client.close()
p = patch(
"google.cloud.datastore.client.Query",
spec=["__call__", "fetch"],
)
with p as mock_klass:
mock_klass.fetch.return_value = [1, 2] # This line seems to have no effect?
foo()
mock_klass.fetch.assert_called_once()
When I however execute the code, assert_called_once
fails.
I seem to be doing something wrong when mocking but cannot figure out how to get it working. Would greatly appreciate if someone could indicate how to get this to work.
Upvotes: 0
Views: 679
Reputation: 14052
Needed to replace mock_klass.fetch
with mock_klass.return_value.fetch
.
Upvotes: 1