<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><div style="text-align:left;direction:ltr"><span style>Hi</span></div><div style="text-align:left;direction:ltr"><span style>1) I prefer to use start/stop and not the decorator .</span></div>
<div style="text-align:left;direction:ltr"><span style>2) mock_play is the name of the module where the code belongs</span></div><div style="text-align:left;direction:ltr"><span style><br></span></div><div style="text-align:left;direction:ltr">
<span style>Thanks</span></div><div style="text-align:left;direction:ltr"><span style><br></span></div><div style="text-align:left;direction:ltr"><span style>Avishay</span></div><br><span style>Sent from my iPhone</span></div>
<div style><br>On 7 באוג 2013, at 21:01, Jean-Michel Pichavant <<a href="mailto:jeanmichel@sequans.com">jeanmichel@sequans.com</a>> wrote:<br><br></div><blockquote type="cite" style><div><span>----- Mail original -----</span><br>
<blockquote type="cite"><span>Hi</span><br></blockquote><blockquote type="cite"><span>I would like to mock patch the attribute 'calc' in the 'Client' class</span><br></blockquote><blockquote type="cite"><span>(See code below).</span><br>
</blockquote><blockquote type="cite"><span>I have 2 unit tests:</span><br></blockquote><blockquote type="cite"><span>1) test1 -  that patch an existing instance of 'Client' - it works</span><br></blockquote><blockquote type="cite">
<span>fine.</span><br></blockquote><blockquote type="cite"><span>1) test2 -  that tries to patch the 'Client' class. My expectation is</span><br></blockquote><blockquote type="cite"><span>that after the patching, every instance of 'Client' will be created</span><br>
</blockquote><blockquote type="cite"><span>with 'MockClient'. However this is not the case..</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Can you please advice?</span><br>
</blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Thanks</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Avishay</span><br>
</blockquote><span></span><br><span></span><br><span>One way to do this is to decorate the test2 method, <a href="http://www.voidspace.org.uk/python/mock/patch.html">http://www.voidspace.org.uk/python/mock/patch.html</a>.</span><br>
<span>This way you get rid of all the start/stop boiler-plate, the scope of your patch is the scope of the method.</span><br><span></span><br><span>*code not tested*</span><br><span></span><br><span>class TestIt(unittest.TestCase):</span><br>
<span>    def setUp(self):</span><br><span>        pass    </span><br><span></span><br><span>    @mock.patch(Calc, MockCalc)</span><br><span>    def test2(self):</span><br><span>        client = Client()</span><br><span>        # result should be 7</span><br>
<span>        print str(client.add(1,34))</span><br><span></span><br><span>    def test3(self):</span><br><span>        client = Client()</span><br><span>        # result should be 35 again</span><br><span>        print str(client.add(1,34)) </span><br>
<span></span><br><span>By the way, what is 'mock_play' in your original post, could be the reason why you things did go wrong.</span><br><span></span><br><span>JM</span><br><span></span><br><span></span><br><span>-- IMPORTANT NOTICE: </span><br>
<span></span><br><span>The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.</span><br>
</div></blockquote></body></html>