<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    On 15/03/2012 18:30, Mathieu Drapeau wrote:
    <blockquote
cite="mid:CAPar9tbUuWEAC+m+Sqr0CS3PGY8MUMQi=ULJoF9=gcd9mLa-eA@mail.gmail.com"
      type="cite">Hi all,<br>
      I am fairly new to mock and I would like to know how to fix an
      issue during module imports and scope of functions.<br>
      Here is a snippet that explain problem I am dealing with:<br>
      <br>
      from mock import MagicMock<br>
      import sys<br>
      sys.func = lambda: 'x'<br>
      from sys import func<br>
      print func()<br>
      # outputs 'x'<br>
      sys.func = MagicMock(return_value='mocked_x')<br>
      print func()<br>
      # outputs 'x', it is not mocked<br>
      <br>
      How could I fix this?<br>
      <br>
      Thanks,<br>
      Mat<br>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
testing-in-python mailing list
<a class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>
<a class="moz-txt-link-freetext" href="http://lists.idyll.org/listinfo/testing-in-python">http://lists.idyll.org/listinfo/testing-in-python</a>
</pre>
    </blockquote>
    <br>
    Hey Mathieu,<br>
    <br>
    Your 7th line changes the value of sys.func (the 'func' attribute on
    local variable 'sys') but you haven't changed the value of local
    variable 'func'. Modifying one won't affect the other. You'd see the
    same behaviour even if MagicMock wasn't involved (e.g assigning
    'sys.func' to 0 will not affect the value of 'func' either.)<br>
    <br>
    From the code you've posted, it's not clear to me what you're trying
    to achieve.<br>
    <br>
    Perhaps you meant to write:<br>
    <br>
    &gt;&gt;&gt; func = Mock(return_value='mocked')<br>
    &gt;&gt;&gt; func()<br>
    mocked<br>
    <br>
    or<br>
    <br>
    &gt;&gt;&gt; sys.func = Mock(return_value='mocked')<br>
    &gt;&gt;&gt; sys.func()<br>
    mocked<br>
    <br>
    Best regards,<br>
    <br>
    &nbsp;&nbsp;&nbsp; Jonathan<br>
    <br>
    <pre class="moz-signature" cols="72">-- 
Jonathan Hartley    <a class="moz-txt-link-abbreviated" href="mailto:tartley@tartley.com">tartley@tartley.com</a>    <a class="moz-txt-link-freetext" href="http://tartley.com">http://tartley.com</a>
Made of meat.       +44 7737 062 225       twitter/skype: tartley

</pre>
  </body>
</html>