unit testing - Python mock return of a getattr() call -


edit: might falling xy problem trap here. here's want know.

i have following function:

def foo():   bar = functogetbar()   return bar.getattr("some_attr", none) 

in test, try do:

mocked_bar = magicmock() expected_return_val = [obj1, obj2, ...] functogetbar.return_value = mocked_bar  # functogetbar patched  def testgetbar(self):   assertequal(expected_return_val, foo()) 

now want provide expected_return_val onto some_attr attribute on bar object.

i have tried using propertymock:

type(mocked_bar).getattr = propertymock(return_value=expected_return_value) 

running test error:

typeerror: 'list' object not callable 

if return_value set scalar (not list), result of getattr call in real function mock, , not scalar provided.

also, i'm worried mocking out getattr method, since common method used elsewhere in function. how can set list on attribute of mock object under test?


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -