<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
El 10/11/11 09:24, Sylvain Viollon escribió:
<blockquote
cite="mid:60E9C462-D713-49AE-81E6-4A2D4190025F@infrae.com"
type="cite">
<pre wrap="">
Op 9 nov 2011, om 23:16 heeft Àlex Magaz Graça het volgende geschreven:
</pre>
<blockquote type="cite">
<pre wrap="">Hello,
</pre>
</blockquote>
<pre wrap="">
Hello,
</pre>
<blockquote type="cite">
<pre wrap="">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:
<a class="moz-txt-link-freetext" href="http://grok.zope.org/doc/current/reference/components.html#grok.index.Value">http://grok.zope.org/doc/current/reference/components.html#grok.index.Value</a>
</pre>
</blockquote>
<pre wrap="">
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
</pre>
</blockquote>
<font face="sans-serif"><br>
Thanks for your quick response! <br>
<br>
I've been trying but still no luck :/<br>
<br>
First I've added a setUp() method with a call to setSite() like
this (where Helpdesk is a grok.Application):<br>
<br>
class TicketCatalogTest(unittest.TestCase):<br>
def setUp(self):<br>
helpdesk = Helpdesk()<br>
self._catalog = helpdesk.ticketCatalog<br>
setSite(helpdesk)<br>
<br>
but I get this:<br>
<br>
Error in test testGetActiveTickets
(helpdesk.tests.test_helpdesk.TicketCatalogTest)<br>
Traceback (most recent call last):<br>
File "/usr/lib/python2.7/unittest/case.py", line 312, in run<br>
self.setUp()<br>
File
"/home/alex/uni/projecte/repo/list-active-tickets/HelpDesk/src/helpdesk/tests/test_helpdesk.py",
line 23, in setUp<br>
setSite(helpdesk)<br>
File
"/home/alex/.buildout/eggs/zope.component-3.10.0-py2.7.egg/zope/component/hooks.py",
line 67, in setSite<br>
sm = site.getSiteManager()<br>
File
"/home/alex/.buildout/eggs/zope.site-3.9.2-py2.7.egg/zope/site/site.py",
line 76, in getSiteManager<br>
raise ComponentLookupError('no site manager defined')<br>
ComponentLookupError: no site manager defined<br>
<br>
Then, greping around in ~/.buildout I found some tests which set a
site manager, so I end up with the following code:<br>
<br>
def setUp(self):<br>
helpdesk = Helpdesk()<br>
self._catalog = helpdesk.ticketCatalog<br>
<br>
setHooks()<br>
provideAdapter(<br>
SiteManagerAdapter, (Interface,), IComponentLookup)<br>
site = rootFolder()<br>
site.setSiteManager(LocalSiteManager(site))<br>
setSite(site)<br>
<br>
But then again the look up error:<br>
<br>
Error in test testGetActiveTickets
(helpdesk.tests.test_helpdesk.TicketCatalogTest)<br>
Traceback (most recent call last):<br>
File "/usr/lib/python2.7/unittest/case.py", line 321, in run<br>
testMethod()<br>
File
"/home/alex/uni/projecte/repo/list-active-tickets/HelpDesk/src/helpdesk/tests/test_helpdesk.py",
line 45, in testGetActiveTickets<br>
activeTickets = self._catalog.getActiveTickets()<br>
File
"/home/alex/uni/projecte/repo/list-active-tickets/HelpDesk/src/helpdesk/app.py",
line 31, in getActiveTickets<br>
catalog = getUtility(ICatalog)<br>
File
"/home/alex/.buildout/eggs/zope.component-3.10.0-py2.7.egg/zope/component/_api.py",
line 169, in getUtility<br>
raise ComponentLookupError(interface, name)<br>
ComponentLookupError: (<InterfaceClass
zope.catalog.interfaces.ICatalog>, '')<br>
<br>
I've tried several variations of this codes, but there's always
something that fails. So I'm back to the beginning.<br>
<br>
Any idea of what is missing? A link to documentation about this or
code examples would be very useful. I didn't find anything.<br>
<br>
Thanks,<br>
Àlex<br>
</font>
</body>
</html>