简体   繁体   中英

How to mock a class attribute in Python Mockito

I'm writing test cases using Mockito. My code looks like this

def function_to_test():
    dog = lib1.get_dog()
    return dog.weight

My test code looks like this

def test_method1():
    dog_mock = mock()
    when(lib1).get_dog().thenReturn(dog_mock)
    when(dog).weight.thenReturn(10)  # <-- I'm not sure how do I write this?

As dog.weight is not a method. How do I mock it?

Not familiar with Mockito framework for Python, but I think you should just set the property on the mock object. Something like that:

dog_mock = mock()
dog_mock.weight = 100
when(lib1).get_dog().thenReturn(dog_mock)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM