On Mar 27, 2010, at 5:01 PM, Mike Orr wrote:
> On Sat, Mar 27, 2010 at 3:32 PM, Ben Bangert <[email protected]> wrote:
>> On Mar 27, 2010, at 3:40 AM, grassoalvaro wrote:
>>
>>> i have problem with some automatic tests. Simple example:
>>>
>>> [code]
>>>
>>> from test.tests import *
>>> from pylons import tmpl_context as c
>>>
>>> class TestBlaController(TestController):
>>>
>>> def test_index(self):
>>> response = self.app.get(url(controller='bla', action='index'))
>>> c.test = 'dupa'
>>
>> Per the testing documentation:
>> http://pylonshq.com/docs/en/1.0/testing/#testing-pylons-objects
>>
>> The Pylons globals are *not* available outside a request. You will need to
>> utilize response.tmpl_context after the request, which is the actual object
>> that was used during that request. (Assuming you're using a Pylons 1.0RC1
>> project).
>
> What about pylons.url? You need that before you make a request.
The default project templates dumps a file __init__.py like so into
PROJ/PROJ/tests/__init__.py:
from unittest import TestCase
from paste.deploy import loadapp
from paste.script.appinstall import SetupCommand
from pylons import url
from routes.util import URLGenerator
from webtest import TestApp
import pylons.test
__all__ = ['environ', 'url', 'TestController']
# Invoke websetup with the current config file
SetupCommand('setup-app').run([pylons.test.pylonsapp.config['__file__']])
environ = {}
class TestController(TestCase):
def __init__(self, *args, **kwargs):
wsgiapp = pylons.test.pylonsapp
config = wsgiapp.config
self.app = TestApp(wsgiapp)
url._push_object(URLGenerator(config['routes.map'], environ))
TestCase.__init__(self, *args, **kwargs)
Since your tests inherit from TestController, you could easily just add a
self.config reference, and we see that the url global object is prepped there
with a URLGenerator. This is why in the example above, where url() is being
used, that works. Though url.current of course wouldn't work since its being
passed an empty environ.
Cheers,
Ben
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.