Opened 15 years ago

Closed 14 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: no

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

Download all attachments as: .zip

Change History (7)

comment:1 by Carl Meyer, 15 years ago

Easy pickings: unset
Triage Stage: UnreviewedAccepted

comment:2 by mmcnickle, 15 years ago

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

by mmcnickle, 15 years ago

Attachment: 15826.diff added

Render TemplateReponse inside assertContains

by mmcnickle, 15 years ago

Attachment: 15826.2.diff added

BLANK -- do not use

comment:3 by mmcnickle, 15 years ago

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?

in reply to:  3 comment:4 by Carl Meyer, 15 years ago

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 by Julien Phalip, 14 years ago

Resolution: fixed
Status: assignedclosed

In [17025]:

Fixed #15826 -- Made assertContains and assertNotContains work with SimpleTemplateResponse by forcing it to be rendered if it hasn't been rendered yet. Thanks to bmihelac for the report, to mmcnickle for the initial patch and tests, and to Carl Meyer for the guidance.

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