could just as easily pass in a callable. In that case your view would
look something like this...
def suggest(request, usrid=None, get_friends=__get_friends):
current_user = request.facebook.user
facebook_friends = get_friends(current_user)
..and your test would look something like this...
def pretend_get_friends(current_user):
return [(2, 'John Doe'), (4, 'Jane Doe'), (5, 'The Frog')]
def test_suggest(self):
request = some_hand_crafted_request()
response = suggest(request, get_friends=pretend_get_friends)
self.assertEqual(response.status_code, 200) # or whatever
--Stuart
On Oct 4, 9:18 am, Stuart <stu...@bistrotech.net> wrote:
> Here is one approach...
>
> class FBFriends:
> def get_friends(self, current_user):
> return __get_friends(current_user) # heavy call
>
> def suggest(request, usrid=None, friend_source=FBFriends()):
> current_user = request.facebook.user
> facebook_friends = friend_source.get_friends(current_user)
>
> Meanwhile, in tests.py...
>
> class FBFriendsMock:
> def get_friends(self, current_user):
> return [(2, 'John Doe'), (4, 'Jane Doe'), (5, 'The Frog')]
>
> def test_suggest(self):
> request = some_hand_crafted_request()
> response = suggest(request, friend_source=FBFriendsMock())
> self.assertEqual(response.status_code, 200) # or whatever
>
> --Stuart
>
> On Oct 4, 7:34 am, Reikje <reik.sch...@gmail.com> wrote:
>
>
>
>
>
>
>
> > In one of my views, I am doing a call to the Facebook graph API which
> > is a bit heavyweight and you also need a valid token. I am looking
> > into ways to use mocking/dependency injection to avoid having to do
> > this call during view tests. So let's say i have this view method:
>
> > def suggest(request, usrid = None):
> > current_user = request.facebook.user
> > facebook_friends = __get_friends(current_user) # heavy call
>
> > during testing I want to use something simple:
>
> > def suggest(request, usrid = None):
> > current_user = request.facebook.user
> > facebook_friends = [(2, 'John Doe'), (4, 'Jane Doe'), (5, 'The
> > Frog')]
>
> > Any suggestion how to do mocking/DI from a view?
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment