[TIP] How do I mock future instances of a class?
yonix85 at gmail.com
Wed May 25 03:57:13 PDT 2011
OK I actually find a nice way of solving it:
wrapper_self = self
self.patcher = patch("package.MyClassName", new = MyFakeClass)
self.ret_val = "xyz"
If you have a better way, please share :)
On Wed, May 25, 2011 at 1:39 PM, Yoni Tsafir <yonix85 at gmail.com> wrote:
> Sorry for spamming with mock questions the last few days :)
> Just doing some massive testing for complicated code.
> I know that when I patch a specific method:
> @patch("package.MyClassName.my_method", new = Mock(return_value="xyz"))
> Even new instances of MyClass will return "xyz" as expected.
> However, when I try:
> def test_something(self, my_mock):
> my_mock.my_method.return_value = "xyz"
> When in code I run:
> I get a new different Mock and not "xyz" as I would wish.
> Now, it kinda makes sense to me, but I would like to know if there's a way
> around it.
> Couldn't really achieve this by mocking __init__ (or maybe I did it wrong?)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the testing-in-python