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

Wichert Akkerman wichert at wiggy.net
Wed Apr 8 10:11:47 EDT 2009


On 4/8/09 4:07 PM, Uli Fouquet wrote:
> Hi there,
>
> Wichert Akkerman wrote:
>    
>> How can you unittest a view? Trying to instantiate a view in a testcase
>> fails when the constructor tries to access self.__view_name__ and
>> self.module_info.
>>      
>
> Maybe something like this?
>
> :test-layer: unit
>
>      >>>  import grok
>      >>>  grok.testing.grok('sample')
>    

That groks the whole sample package, right? I do not want that, since 
that can have all kinds of effects. What I am looking for is the very 
minimal thing that allows me to instantiate a view so I can call methods 
on it. Currently I use this:

     def setUp(self):
         # grok makes unit testing extremely painful
         View.__view_name__ = "iew"
         View.module_info=Mock()
         View.module_info.package_dotted_name="euphorie.content.model.View"

     def tearDown(self):
         del View.__view_name__
         del View.module_info

     def testSomething(self):
         model=Model()
         view=View(model, None)
         # Do the tests here


which works, but is not quite optimal

Wichert.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/grok-dev/attachments/20090408/79591dbb/attachment-0001.html 


More information about the Grok-dev mailing list