Skip to content

Commit df776d2

Browse files
mfatimb07
authored andcommitted
doc: example for usage of rf, admin_user and class-based views (#480)
1 parent befbf37 commit df776d2

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

docs/helpers.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ case there is no "admin" user yet).
191191
As an extra bonus this will automatically mark the database using the
192192
``django_db`` mark.
193193

194+
194195
``django_user_model``
195196
~~~~~~~~~~~~~~~~~~~~~
196197

@@ -308,3 +309,43 @@ Clearing of mail.outbox
308309
``mail.outbox`` will be cleared for each pytest, to give tests a empty
309310
mailbox. It is however more pytestic to use the ``mailoutbox`` fixture
310311
to access ``mail.outbox``.
312+
313+
314+
Examples
315+
--------
316+
317+
318+
Example with ``rf``, ``admin_user``, fixture and class-based views
319+
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
320+
321+
::
322+
323+
import pytest
324+
325+
from django.core.urlresolvers import reverse
326+
from myapp.models import Thing
327+
from myapp.views import ThingDetailView
328+
329+
@pytest.fixture
330+
def thing(admin_user):
331+
(thing_object, created) = Thing.objects.get_or_create(name="test",
332+
created_by=admin_user)
333+
return thing_object
334+
335+
def test_detail_view_logged_in(rf, admin_user, thing):
336+
# set kwargs for reverse and for view
337+
kwargs_thing = {
338+
'pk': thing.id,
339+
}
340+
341+
url = reverse("thing_detail", kwargs=kwargs_thing)
342+
343+
# bind url to request factory
344+
request = rf.get(url)
345+
346+
# set user in request to admin_user
347+
request.user = admin_user
348+
349+
# creates response, given request and kwargs needed for view
350+
response = ThingDetailView.as_view()(request, **kwargs_thing)
351+
assert response.status_code == 200

0 commit comments

Comments
 (0)