[Grok-dev] getUtility(ICatalog) fails with ComponentLookupError
uli at gnufix.de
Fri Nov 11 12:28:20 UTC 2011
Am Freitag, den 11.11.2011, 09:14 +0100 schrieb Christian Klinger:
> what kind of testsetup do you use in your unittest?
> Do you use the default setup?
From the snippets sent by Alex I get the impression that the test setup
is rather plain: no layer, no 'FunctionalTestCase' of any kind, just
In case of catalogs that quickly means trouble. While Sylvains hint to
use setSite() is correct, there are still some caveats to consider:
Catalogs are created automatically when you add a grok.Application
instance (like a HelpDesk instance) to the ZODB. The catalog creation is
triggered by an event handler for IObjectAddedEvent.
So, there should be no need to create the catalog in tests manually.
To have a ZODB available during testing, you certainly want some kind of
'functional' test setup. What you need then, is some real 'root' meaning
the root object of the ZODB activated during tests. This is normally the
main thing created by functional test setups (beside registrations and
If you have this root, you should be able to follow the examples from
docs as stated:
>>> root['app'] = HelpDesk()
>>> from zope.component.hooks import setSite
and also the catalog lookup should work.
By the way: if you use `bin/interactive_debugger` instead of
`bin/python_console` you should get a functional environment around your
Python shell where you can do things like above as you like. One main
difference is: you get a 'root' of ZODB and all components defined in
your code (like the indexes etc.) are registered on startup of this
'high-level' console. The interactive debugger has access to the local
ZODB (but be aware that you work on the real ZODB data there).
A nice side-effect if you do 'functional' testing is that new objects
added to the ZODB (yes, they won't be catalogued before put in the ZODB,
because catalogs also rely on IObjectAddedEvents which are triggered by
ZODB additions) will be searchable afterwards.
Otherwise - after enourmous efforts to make your catalog findable in a
pure unittest.TestCase environment - you might hit the next big problem:
your objects won't be catalogued as expected.
I admit that setting up catalogs and their persistence is really
difficult or even nasty to manage sometimes (just think of the problems
when trying to add/remove indexes from existing catalogs), but for now I
don't know of something better.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.zope.org/pipermail/grok-dev/attachments/20111111/279b6d07/attachment.bin
More information about the Grok-dev