[Grok-dev] How does one unittest a view?

Martin Aspeli optilude+lists at gmail.com
Thu Apr 9 04:55:41 EDT 2009


Wichert Akkerman wrote:
> Previously Jan-Wijbrand Kolman wrote:
>> Would just grokking the View component itself be acceptable?
>>
>> If so, I think you could do:
>>
>>   from grokcore.component.testing import grok_component
>>
>>   def setUp(self):
>>     grok_component('View', View)
>>
>> But this still involves having CA registrations taking place and it
>> seems that that is not what you want *at all*, right?
> 
> Preferably not.
> 
>> In that case yes, Grok makes it not easy to write a "pure" unittests for
>> view components.
>>
>> I wonder how this would work in a vanilla Zope 3 context though...
> 
> One uses zope.component.testing.PlacelessSetup and manually calls
> provideAdapter & friends when needed.

placelessSetUp registers a baseline of components needed to do things. 
Basically, using a layer with placelessSetUp and provideAdapter on your 
view is equiavlent to doing a grok() on grokcore.view.meta and a 
grok_component() on your own component.

However, I *also* think it'd be sensible for grokcore.view.View to allow 
instantiation as a simple class. The reason it fails is that it relies 
on an attribute that's set during grokking, AFAIK to set up the 'static' 
resource directory. If it would just fall back to None or work 
gracefully in this case, it'd be easier to test.

Martin

-- 
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book



More information about the Grok-dev mailing list