request.current_route_url() raise Error in a ViewIntegrationTests context

92 views
Skip to first unread message

Emmanuel Cazenave

unread,
Dec 6, 2012, 5:45:35 AM12/6/12
to pylons-...@googlegroups.com
Hi,

In a view I use request.current_route_url() because I want it in the returned dict.

@view_config(route_name='my_view', renderer='templates/my_view.pt')
def my_view(request):
   ....
   ....
   return dict(....,url=request.current_route_url())

It works properly in a 'production' context but raises an exception in a unittest:

class ViewIntegretionTest(unittest.TestCase):

    def setUp(self):
        self.request = testing.DummyRequest()
        self.config = testing.setUp(request=self.request)
        from sqlalchemy import create_engine
        engine = create_engine('sqlite://')
        DBSession.configure(bind=engine)
        Base.metadata.create_all(engine)
  
   def test_my_view(self):
        def callable(config):
            config.add_route('my_view', '/my_view')
        self.config.include(callable)
        from myapp.views import my_view
        response = my_view(self.request)
      
    def tearDown(self):
        DBSession.remove()
        testing.tearDown()

This raises the following exception:

Traceback (most recent call last):
  File "d:\envs\myapp_env\myapp_repo\myapp\tests\test_views\tes
t_myview.py", line 16, in test_my_view
    response = my_view(self.request)
  File "d:\envs\myapp_env\myapp_repo\myapp\views.py", line 42,
in my_view
    request.current_route_url()
  File "d:\envs\myapp_env\pyramid-1.3\pyramid\url.py", line 677, in curre
nt_route_url
    raise ValueError('Current request matches no route')
ValueError: Current request matches no route

I suppose I don't initialize the testing environment properly.......any help ?

Thanks.

Emmanuel Cazenave

unread,
Dec 7, 2012, 8:03:20 AM12/7/12
to pylons-...@googlegroups.com
Ok I manage it using request.route_url('my_view') instead of request.current_route_url().

I assume this is because request.route_url ask the Configurator which url a given route has, whereas request.current_route_url needs the requested to were routed which is not the case in a ViewIntegretionTest.

But if my assomptions are right, does it means we can't perform an integretion test on a view callable that uses request.current_route_url ?
Or is there a way to mock the routing operations by setting some attributes of the DummyRequest ?

Any help appreciated.

Thanks.
Reply all
Reply to author
Forward
0 new messages