On Sat, Sep 17, 2011 at 7:47 AM, Reikje <reik.sch...@gmail.com> wrote:
> Okay I am having some troubles getting this to work. In my application
> I am using WebTest and django-webtest to do template testing. In order
> to monkeypatch the django.core.urlresolvers.reverse function, I have
> created a new Test base that looks like this:
>
> class FacebookWebTest(WebTest):
>
>    def __init__(self, methodName='runTest'):
>        super(FacebookWebTest, self).__init__(methodName)
>        import django.core.urlresolvers
>        django.core.urlresolvers.reverse = self.reverse
>
>    def reverse(*args, **kwargs):
>        return "foo"
>
>
> Then hier is a test:
>
> from django.core.urlresolvers import reverse
>
> class HomepageViewTestCase(FacebookWebTest):
>
>    def testPopulateFieldsInitially(self):
>        url = reverse('webapp_home')
>        form = self.app.get(url).form
>        ...
>
> Within the HomepageViewTestCase it is still calling the original
> reverse method in django.core.urlresolvers even though it is calling
> the FacebookWebTest constructor before. Any suggestions :)

I usually monkey patch like so:

# myviews.py

from django.core.urlresolvers import reverse

def view(request):
     blah blah
     x = reverse('whaterrr')
     return HttpResponse('monkey-patch')

# tests.py

import myviews

def patch_reverse(name):
     return whatever_you_want

class MyViewTests(TestCase):

    def setUp(self):
        self.old_reverse = getattr(myviews, 'reverse')
        myviews.reverse = patch_reverse

    def tearDown(self):
        myviews.reverse = self.old_reverse

Gets the job done

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

Reply via email to