Opened 4 years ago

Closed 4 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 4 years ago.
Render TemplateReponse inside assertContains
15826.2.diff (2 bytes) - added by mmcnickle 4 years ago.
BLANK -- do not use

Download all attachments as: .zip

Change History (7)

comment:1 Changed 4 years ago by carljm

  • Easy pickings unset
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 4 years ago by mmcnickle

  • Cc mmcnickle added
  • Has patch set
  • Owner changed from nobody to mmcnickle
  • Status changed from new to assigned

Changed 4 years ago by mmcnickle

Render TemplateReponse inside assertContains

Changed 4 years ago by mmcnickle

BLANK -- do not use

comment:3 follow-up: Changed 4 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 4 years ago by carljm

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 4 years ago by julien

  • Resolution set to fixed
  • Status changed from assigned to closed

In [17025]:

(The changeset message doesn't reference this ticket)

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