Opened 6 years ago

Closed 5 years ago

#15826 closed Bug (fixed)

TestCase assertions should work with TemplateResponse

Reported by: bmihelac Owned by: mmcnickle
Component: Testing framework Version: 1.3
Severity: Normal Keywords:
Cc: mmcnickle Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX:

Description

When using assertContains for testing class based generic views or other views that returns TemplateResponse, following error is raised:

ContentNotRenderedError: The response content must be rendered before it can be accessed.

Example:

        response = SomeView.as_view()(request)
        self.assertContains(response, 'some content')

This can be avoided with adding response.render() before assertion is called, but in my opinion assertContain should check if content is rendered instead.

Attachments (2)

15826.diff (3.1 KB) - added by mmcnickle 6 years ago.
Render TemplateReponse inside assertContains
15826.2.diff (2 bytes) - added by mmcnickle 6 years ago.
BLANK -- do not use

Download all attachments as: .zip

Change History (7)

comment:1 Changed 6 years ago by Carl Meyer

Easy pickings: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 6 years ago by mmcnickle

Cc: mmcnickle added
Has patch: set
Owner: changed from nobody to mmcnickle
Status: newassigned

Changed 6 years ago by mmcnickle

Attachment: 15826.diff added

Render TemplateReponse inside assertContains

Changed 6 years ago by mmcnickle

Attachment: 15826.2.diff added

BLANK -- do not use

comment:3 Changed 6 years ago by mmcnickle

I added a check if the response handed to assertContains is a TemplateResponse, and if so, will render it.
Added the same check to assertNotContains for consistency.

I do an explicit check for instanceof(response, SimpleTemplateResponse), but perhaps duck typing for any response with response.render() would be more appropriate/useful?

comment:4 in reply to:  3 Changed 6 years ago by Carl Meyer

Replying to mmcnickle:

I do an explicit check for instanceof(response, SimpleTemplateResponse), but perhaps duck typing for any response with response.render() would be more appropriate/useful?

Yes, I think it'd be better to check for a callable response.render() rather than using isinstance. This (checking for the render method) is what Django already does in its normal response handling, after template response middleware.

comment:5 Changed 5 years ago by Julien Phalip

Resolution: fixed
Status: assignedclosed

In [17025]:

(The changeset message doesn't reference this ticket)

Note: See TracTickets for help on using tickets.
Back to Top