I have a class
class X:
def __init__(self, db):
self.db = db
def get_data_from_friend(self):
return None
def get_data_from_db(self):
return self.db.get_my_db_data()
def get_data(self):
if data := self.get_data_from_friend():
return data
return self.get_data_from_db()
And I'm trying to test the get_data
method, to validate that the calls inside of it are executed.
I have a test like this
def test_get_data(self):
mock = create_autospec(X)
mock.get_data()
mock.get_data.assert_called_once() # <-- works
mock.get_data_from_friend.assert_called_once() # <-- assertionError, not called
what am I missing here?
db
). You should be free to refactor inside that class, as long as the observable behaviour through the public API remains the same, supported by tests not constantly fighting them. Note also thatmock.get_data()
thenmock.get_data.assert_called_once()
is testing the test, not the implementation - there's no real code involved.X
, but there's so little context left it's impossible to say how exactly.get_data()
?get_data
on a mock, it just records details of what it was called with and returns a canned value). Mockdb
, testX
.X
instance in your test at all.