Is it possible to have a side_effect on a property? If I look at the Mock documentation it seems it's only possible on object methods.
I am trying to test the following:
def get_object(self): try: return self.request.user.shop except Shop.DoesNotExist: return None
I want Shop to raise a DoesNotExist exception.
Guess maybe I wasn't clear enough but I am talking about the voidspace mock library.
http://www.voidspace.org.uk/python/mock/index.html
side_effect: A function to be called whenever the Mock is called. See the side_effect attribute. Useful for raising exceptions or dynamically changing return values. The function is called with the same arguments as the mock, and unless it returns DEFAULT , the return value of this function is used as the return value.
Mock vs. So what is the difference between them? MagicMock is a subclass of Mock . It contains all magic methods pre-created and ready to use (e.g. __str__ , __len__ , etc.). Therefore, you should use MagicMock when you need magic methods, and Mock if you don't need them.
It's worth noting that there is now the PropertyMock
class:
>>> m = MagicMock() >>> p = PropertyMock(side_effect=ValueError) >>> type(m).foo = p >>> m.foo Traceback (most recent call last): .... ValueError
That example was taken from the official site.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With