Suppose, I have a code snippet as
foo = SomeClass()
bar = foo[1:999].execute()
To test this, I have tried something as
foo_mock = Mock()
foo_mock[1:999].execute()
Unfortunately, this raised an exception,
TypeError: 'Mock' object is not subscriptable
So, How can I create a subscriptable Mock
object?
Just use a
MagicMock
instead.It's so called "magic" because it supports
__magic__
methods such as__getitem__
.