How can I mock a property to raise an exception?

Viewed 3061

I'm using MagicMock with Python 2.7 to mock objects. One of the classes that I'm mocking has properties, one of which can raise a TypeError in some cases.

I would like to mock that behavior, but I can't figure out how:

  • del my_mock.my_property will cause an AttributeError if my_property is accessed, but I need a TypeError.
  • my_mock.my_property = MagicMock(side_effect=TypeError) causes a TypeError when my_property is called, but not when it's merely accessed.

How would I do that?

1 Answers
Related