sorenisanerd / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

AttributeError: _mock_methods after upgrade python-mock from 0.6 to 0.8 #161

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Have a class inherited from Mock in unittest.
2. Define a parameter in __init__() of that class, being it instance of  
another class inherited from mock.
3. Run the unittest. Works in 0.6, doesn't work in 0.8.

What is the expected output? What do you see instead?
Desired output is my unittests passing.
I get AttributeError: _mock_methods instead.

What version of the product are you using? On what operating system?
Python mock 0.8 (resp 0.6), Debian testing x86_64

Please provide any additional information below.

Upgrading from mock 0.6 to 0.8 brakes my unittests.

This is maybe my "not_understanding" how `spec` works, or rather how to get rid 
of it.

What makes me think that this is a bug is a stange error message: 
AttributeError: _mock_methods

My code which causes this is as follows:

  class LoggerMock(Mock):

    def debug(self,argument):
        #027 Printing debug messages disabled. 
        pass

  class DebugMock(Mock):
    mainLogger=None
    ThreadAwareLogger=None

    def __init__(self):
        self.mainLogger=LoggerMock()
        self.ThreadAwareLogger=LoggerMock()

======================================================================
ERROR: test_BOWBuilderComplex_produce_data (__main__.BOWBuilderComplexTest)
Into counters goes the result of parser.words()
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./unittest_bow.py", line 153, in setUp
    self.bOWBuilderComplex=BOWBuilderComplexWithDummySettings(None,self.outputQueue,None,outputCondition,parserInstance=self.parserInstance)
  File "/media/KINGSTON/Sumid/src/bow.py", line 73, in __init__
    self.customInitialization()
  File "./unittest_bow.py", line 144, in customInitialization
    self.debug=DebugMock()
  File "./unittest_bow.py", line 56, in __init__
    self.mainLogger=LoggerMock()
  File "/usr/lib/python2.7/dist-packages/mock.py", line 798, in __setattr__
    elif (self._spec_set and self._mock_methods is not None and
  File "/usr/lib/python2.7/dist-packages/mock.py", line 698, in __getattr__
    elif self._mock_methods is not None:
  File "/usr/lib/python2.7/dist-packages/mock.py", line 697, in __getattr__
    raise AttributeError(name)
AttributeError: _mock_methods

$ uname -a
Linux Khedron 3.2.0-2-amd64 #1 SMP Sat May 12 23:08:28 UTC 2012 x86_64 GNU/Linux

$ python --version
Python 2.7.3rc2

$ dpkg -l python-mock
||/ Name                                Version                             
+++-===================================-===================================-
ii  python-mock                         0.8.0-1 

dpkg -l python
||/ Name                                Version                             
+++-===================================-===================================-
ii  python                              2.7.2-10

Original issue reported on code.google.com by Gail.Wyn...@gmail.com on 24 May 2012 at 12:52

GoogleCodeExporter commented 9 years ago
The problem is that your initialisation method doesn't initialise its parents.

Try this: 

  class DebugMock(Mock):
    mainLogger=None
    ThreadAwareLogger=None

    def __init__(self):
        super(DebugMock, self).__init__()        
        self.mainLogger=LoggerMock()
        self.ThreadAwareLogger=LoggerMock()

Original comment by fuzzyman on 24 May 2012 at 1:17

GoogleCodeExporter commented 9 years ago
Yeah, that's it :-) Thanks.
Feel free to close the issue.

Original comment by Gail.Wyn...@gmail.com on 24 May 2012 at 2:30

GoogleCodeExporter commented 9 years ago

Original comment by fuzzyman on 24 May 2012 at 2:32