Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mock Pyramid's `request.matched_route` object?

I'm writing unit tests for my project that uses Pyramid. What I've done so far is add data and attributes manually to the requests. For example setting an id in the route:

request = testing.DummyRequest()
request.matchdict['id'] = 19

One of my views has multiple routes attached to it, and I determine the route with request.matched_route.name.

Now when I try to manually set the route like:

request.matched_route.name = 'one_of_my_routes'

or

request.matched_route = {'name': 'one_of_my_routes'}

I get errors. What is the correct way to test this using Python unit tests?

like image 450
Niel Avatar asked Oct 15 '25 15:10

Niel


1 Answers

Well this isn't Javascript. You can't make a dictionary and expect to use the dot operator on it. So that's why #2 doesn't work. #1 is likely related to the fact that you need to create each object in the chain. request only has a matched_route property if a route matches so you'll need to create that object.

class DummyRoute(object):
    name = 'one_of_my_routes'

request.matched_route = DummyRoute()
like image 159
Michael Merickel Avatar answered Oct 17 '25 06:10

Michael Merickel