dgorur
dgorur

Reputation: 1662

MagicMock and wrap

Why doesn't the "wraps" keyword work consistently for MagicMock objects? Normal methods are passed through to the wrapped object, but not "special" methods. In the test below, the first assertion passes, the second fails.

import mock
import unittest


class Foo(object):
    def bar(self):
        return 1

    def __len__(self):
        return 3


class TestWrap(unittest.TestCase):
    def test(self):
        foo = Foo()
        c = mock.MagicMock(wraps=foo)
        assert c.bar() == 1 # Passes
        assert len(c) == 3 # Fails

I cannot find anything in the docs that suggests this. Am I missing something?

Upvotes: 5

Views: 3845

Answers (1)

TomDotTom
TomDotTom

Reputation: 6744

Because magic methods are looked up differently from normal methods [1], this support has been specially implemented

It appears that the wraps functionality does not wrap the __len__ method for you and you'll have to do it by hand.

Upvotes: 1

Related Questions