[Grok-dev] How does one unittest a view?
Jan-Wijbrand Kolman
janwijbrand at gmail.com
Wed Apr 8 11:05:26 EDT 2009
Wichert Akkerman wrote:
>> 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
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?
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...
regards,
jw
More information about the Grok-dev
mailing list