1

I have a mock object for following class

class Book(object):

    def __init__(self):
        self._counter = 1

    @property
    def counter(self):
        _, self._counter = self._counter, self._counter + 1 
        return _

the class Book have following Mock object

book = Mock(spec = Book, self._counter = 1)

if counter is not a property I could have written test as such

def test_counter(self):
    book.counter = partial(Book.counter, book)
    self.assertNotEquals(book.counter(), book.counter())

however since it is a property, the above strategy won't work, can you suggest a good way to write test for the property given I have to use Mock object for Book class.

1 Answer 1

1
def test_counter(self):
    book.counter = partial(Book.counter.fget, book)
    self.assertNotEquals(book.counter(), book.counter())
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain your answer a little bit.
Actually the @property decorator returns a property object which has a fget function to actually run the underlying function on which decorator was applied

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.