kaste / mockito-python

Mockito is a spying framework
MIT License
123 stars 12 forks source link

How to mock @property #15

Closed aaltat closed 6 years ago

aaltat commented 6 years ago

When I have this:

import unittest

from mockito import unstub

class ClassName(object):

    @property
    def value(self):
        return 42

    def is_value(self):
        if self.value == 42:
            return 'is 42'
        raise ValueError()

class TestClassName(unittest.TestCase):

    def test_foo(self):
        x = ClassName()
        with self.assertRaises(ValueError):
            x.is_value()
        unstub()

if __name__ == '__main__':
    unittest.main()

Is there an easy to way to mock what value property returns?

kaste commented 6 years ago

In general you have to mock the class, t.i. ClassName

So easy-peasy solution here is setattr(ClassName, 'value', 41) or even ClassName.value = 41

aaltat commented 6 years ago

Again I wonder why I did not see that one. But it works, thanks from help.