diff --git a/docs/changelog.txt b/docs/changelog.txt index 015eda0a931933b5b79e7773383d30ff57df8548..2db84519ee840b85d9c3484a8ad5fcb0f9364e19 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -13,6 +13,8 @@ The standard library version! * Support for deleting attributes (accessing deleted attributes will raise an `AttributeError`) * Added the `mock_open` helper function for mocking open as a context manager +* `__class__` is assignable, so a mock can pass an `isinstance` check without + requiring a spec 2012/02/13 Version 0.8.0 diff --git a/mock.py b/mock.py index b6ef6f2757a983f8da0184c07854fd5d2c5fc623..417871ba6a5b4b9f2777e8a264f31a180a5bf4d6 100644 --- a/mock.py +++ b/mock.py @@ -748,6 +748,9 @@ class NonCallableMock(Base): # but not method calls _check_and_set_parent(self, value, None, name) setattr(type(self), name, value) + elif name == '__class__': + self._spec_class = value + return else: if _check_and_set_parent(self, value, name, name): self._mock_children[name] = value diff --git a/tests/testmock.py b/tests/testmock.py index 5e0a1426c93792a21ba9cf75ee380acf3c30e02c..0517eacf888f69d56992a3e5a42a6db6666f1593 100644 --- a/tests/testmock.py +++ b/tests/testmock.py @@ -1303,6 +1303,13 @@ class MockTest(unittest2.TestCase): self.assertRaises(AttributeError, getattr, mock, 'f') + def test_class_assignable(self): + for mock in Mock(), MagicMock(): + self.assertNotIsInstance(mock, int) + + mock.__class__ = int + self.assertIsInstance(mock, int) + if __name__ == '__main__': unittest2.main()