Am Montag, 4. Juni 2007 schrieb Marius Gedminas:
> On Mon, Jun 04, 2007 at 03:14:16PM +0200, Florian Lindner wrote:
> > Hello,
> > in a doctest I have an object which has a view registered.
> > I want to call this view and test for the XML it returns.
> > How can I call the view so that it is being rendered, just like called by
> > a browser?
> Is that in a unit test, or a functional test?
> In a unit test you can do it like this:
>     >>> context = ...yourcontentobject...
>     >>> from zope.publisher.browser import TestRequest
>     >>> request = TestRequest()
>     >>> view = MyViewClass(context, request)
>     >>> print view()
>     <some xml or whatever output here>
> If you want to provide, e.g., form parameters, pass them to the request:
>     >>> request = TestRequest(form={'foo': u'Lalala\u1234'})
> If your view does anything interesting (e.g. use TALES expressions, or
> refer to other views like context/@@absolute_url), you will need to
> register a whole bunch of components in your doctest setUp methods.
> Don't forget to tear them down afterwards.  IIRC you will need
>     from import setup
>     def setUp(test):
>         setup.placelessSetUp()
>         setup.setUpTraversal()
>     def tearDown(test):
>         setup.placelessTearDown()
> at the very least.  Accessing other views, resources, or, god forbid,
> forms, will require other component registrations.  At some point you
> have two choices: figure out this stuff once and then use copy & paste
> (actually, helper functions defined once in your project), or switch to
> testing your views with functional tests.

I have thes setup and tearDown methods:

import unittest
import zope.testing.module
from zope.testing import doctest
from zope.component import testing, eventtesting
from import PlacelessSetup
from import setup

container_setup = PlacelessSetup()

def blogSetUp(test):
    zope.testing.module.setUp(test, 'Blog.doctest')

def blogTearDown(test):

and this is my README.txt containing the test:

    >>> context = MyBlog
    >>> from zope.publisher.browser import TestRequest
    >>> from browser.views import RSSFeed
    >>> request = TestRequest()
    >>> view = RSSFeed(context, request)
    >>> print view()

Since my code includes a call to absoluteURL I have added your setup and 
tearDown methods. But there is still an error:

      File "/home/florian/Zope3/src/zope/traversing/browser/", 
line 34, in absoluteURL
        return zope.component.getMultiAdapter((ob, request), IAbsoluteURL)()
      File "/home/florian/Zope3/src/zope/traversing/browser/", 
line 55, in __str__
        raise TypeError(_insufficientContext)
    TypeError: There isn't enough context to get URL information. This is 
probably due to a bug in setting up location information.

Do you know what's missing here?



BTW: What would be the name of the MyViewClass if the page would be registered 
without a class set?
Zope3-users mailing list

Reply via email to