[Grok-dev] getUtility(ICatalog) fails with ComponentLookupError

Àlex Magaz Graça rivaldi8 at gmail.com
Thu Nov 10 23:45:24 UTC 2011


El 10/11/11 09:24, Sylvain Viollon escribió:
> Op 9 nov 2011, om 23:16 heeft Àlex Magaz Graça het volgende geschreven:
>
>> Hello,
>>
>     Hello,
>
>> I have an application with code like this:
>>
>> from zope.component import getUtility
>> from zope.catalog.interfaces import ICatalog
>> ...
>>
>> class TicketCatalog(grok.Container):
>>      ...
>>      def getActiveTickets(self):
>>          catalog = getUtility(ICatalog)
>>          ...
>>
>> after following the code at the end of the example here:
>>
>> http://grok.zope.org/doc/current/reference/components.html#grok.index.Value
>>
>
>      I you call a method of your content that rely on your Grok site directly from a unittest, you need to setup your Grok as a local site for the Zope component architecture. This is automatically done by publisher when your Grok site is traversed in a request.
>
>      In your unittest you need to do before:
>
>      from zope.component.hooks import setSite, getSite
>
>      setSite(self.yourgroksite)
>
>      If you after call getSite(), your Grok must be returned. The setup of your testcase is a good place to do that I think.
>
>      Regards,
>
>      Sylvain
>
>

Thanks for your quick response!

I've been trying but still no luck :/

First I've added a setUp() method with a call to setSite() like this 
(where Helpdesk is a grok.Application):

class TicketCatalogTest(unittest.TestCase):
     def setUp(self):
         helpdesk = Helpdesk()
         self._catalog = helpdesk.ticketCatalog
         setSite(helpdesk)

but I get this:

Error in test testGetActiveTickets 
(helpdesk.tests.test_helpdesk.TicketCatalogTest)
Traceback (most recent call last):
   File "/usr/lib/python2.7/unittest/case.py", line 312, in run
     self.setUp()
   File 
"/home/alex/uni/projecte/repo/list-active-tickets/HelpDesk/src/helpdesk/tests/test_helpdesk.py", 
line 23, in setUp
     setSite(helpdesk)
   File 
"/home/alex/.buildout/eggs/zope.component-3.10.0-py2.7.egg/zope/component/hooks.py", 
line 67, in setSite
     sm = site.getSiteManager()
   File 
"/home/alex/.buildout/eggs/zope.site-3.9.2-py2.7.egg/zope/site/site.py", 
line 76, in getSiteManager
     raise ComponentLookupError('no site manager defined')
ComponentLookupError: no site manager defined

Then, greping around in ~/.buildout I found some tests which set a site 
manager, so I end up with the following code:

     def setUp(self):
         helpdesk = Helpdesk()
         self._catalog = helpdesk.ticketCatalog

         setHooks()
         provideAdapter(
             SiteManagerAdapter, (Interface,), IComponentLookup)
         site = rootFolder()
         site.setSiteManager(LocalSiteManager(site))
         setSite(site)

But then again the look up error:

Error in test testGetActiveTickets 
(helpdesk.tests.test_helpdesk.TicketCatalogTest)
Traceback (most recent call last):
   File "/usr/lib/python2.7/unittest/case.py", line 321, in run
     testMethod()
   File 
"/home/alex/uni/projecte/repo/list-active-tickets/HelpDesk/src/helpdesk/tests/test_helpdesk.py", 
line 45, in testGetActiveTickets
     activeTickets = self._catalog.getActiveTickets()
   File 
"/home/alex/uni/projecte/repo/list-active-tickets/HelpDesk/src/helpdesk/app.py", 
line 31, in getActiveTickets
     catalog = getUtility(ICatalog)
   File 
"/home/alex/.buildout/eggs/zope.component-3.10.0-py2.7.egg/zope/component/_api.py", 
line 169, in getUtility
     raise ComponentLookupError(interface, name)
ComponentLookupError: (<InterfaceClass 
zope.catalog.interfaces.ICatalog>, '')

I've tried several variations of this codes, but there's always 
something that fails. So I'm back to the beginning.

Any idea of what is missing? A link to documentation about this or code 
examples would be very useful. I didn't find anything.

Thanks,
Àlex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/grok-dev/attachments/20111111/2073ad66/attachment.html 


More information about the Grok-dev mailing list