Testing question

45 views
Skip to first unread message

Toni Mueller

unread,
Jun 22, 2012, 5:10:06 PM6/22/12
to pylons-...@googlegroups.com

Hi,

I want to test a view, but the matchdict doesn't get set up. The
application works when I use it via a web browser.


The route configuration:

config.add_route('newsletter_create_issue', '%s/createissue/{templateid}' % root_path)


In the test case (the test suite is set up with their own registry):

def test_getting_an_issue_list(self):
from pyramid import request
# the same code is in the route configuration, with the number being
# a parameter in that view:
path = '/' + root_path + '/createissue/1'
req = request.Request.blank(path)
req.registry = self.config.registry
# The following bails out, due to the code shown below:
result = getNewsletterIssues(req, 1) # BOOM!


def getNewsletterIssues(request, id_):
...
tid = int(request.matchdict['templateid'])


The traceback:

...
tid = int(request.matchdict['templateid'])
TypeError: 'NoneType' object is unsubscriptable


What am I doing wrong?

TIA!



Kind regards,
--Toni++

Joe Dallago

unread,
Jun 22, 2012, 5:37:55 PM6/22/12
to pylons-...@googlegroups.com
You need to populate the matchdict manually.  Add this to your code:

req.matchdict['templateid'] = 1

Pyramid doesn't actually route the URL, so the matchdict isn't populated.


--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.


Joe Dallago

unread,
Jun 22, 2012, 5:39:08 PM6/22/12
to pylons-...@googlegroups.com
Just out of curiosity, why is the _id getting passed into your view as an argument?

Toni Mueller

unread,
Jun 22, 2012, 6:43:11 PM6/22/12
to pylons-...@googlegroups.com

Hi,

On Fri, Jun 22, 2012 at 02:37:55PM -0700, Joe Dallago wrote:
> req.matchdict['templateid'] = 1

ummm... yes, but I thought that it should be created automatically.

> Pyramid doesn't actually route the URL, so the matchdict isn't populated.

Ok, but I didn't find this in the docs. Will have to dig through them
again.


To answer your other question:

That's an artifact after some refactoring. Thanks for pointing it
out to me - it will be deleted.


Kind regards,
--Toni++

Joe Dallago

unread,
Jun 22, 2012, 6:54:43 PM6/22/12
to pylons-...@googlegroups.com
ummm... yes, but I thought that it should be created automatically.

It isn't, someone asked this in IRC the other day, and I just gave you the answer that was given by the leaders of the community.  A discussion about the difference between a "unit" test and a "functional" test ensued. You can read it here.  If you don't like that solution, miranon later chimes in and gives a possible workaround here.  I'm not exactly sure where it is in the docs, please link to it if you find it.



Kind regards,
--Toni++

Reply all
Reply to author
Forward
0 new messages