Tests: How to messages.add_message() before self.client.get() ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Tests: How to messages.add_message() before self.client.get() ?

Carsten Fuchs-4
Dear group,

in my tests I would like to test the processing of messages in the view.

For example:

    def test_is_message_properly_handled(self):
        # The problem: We have no `request` here!
        messages.info(request, 'Hello')
        response = self.client.get("/handling-messages-view/")
        # asserts …

This is an artificial example and I'm aware that I could submit a POST request and then use `follow=True` so that eventually the messages are processed. But I would like to setup messages and their processing independently as shown above.
The problem is that we don't have the `request` instance that is used by the test client.
Is this possible?

Best regards,
Carsten


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/97157990-be0a-39f4-c7d2-a3595b8e2592%40cafu.de.
Reply | Threaded
Open this post in threaded view
|

Re: Tests: How to messages.add_message() before self.client.get() ?

David Nugent
Make your own request.

I don't see the need here but incase I'm missing something: request is just a HTTPRequest instance. You can instantiate it like any other python class. The only downside is that the request you manufacture won't have middleware applied or in fact anything else that is populated and processed before it gets to your handler (headers, method, parsed url parts, content and so on).

This may not actually be an issue in test scenarios, depending on your need, and in this case on the backing store used by the messages framework.  If it is set to session, then you'll need to obtain a session from the client and stick it onto your request object if you want that to persist through your subsequent test requests.

HTH /d

On 10 Nov 2020, at 18:41, Carsten Fuchs <[hidden email]> wrote:

Dear group,

in my tests I would like to test the processing of messages in the view.

For example:

   def test_is_message_properly_handled(self):
       # The problem: We have no `request` here!
       messages.info(request, 'Hello')
       response = self.client.get("/handling-messages-view/")
       # asserts …

This is an artificial example and I'm aware that I could submit a POST request and then use `follow=True` so that eventually the messages are processed. But I would like to setup messages and their processing independently as shown above.
The problem is that we don't have the `request` instance that is used by the test client.
Is this possible?

Best regards,
Carsten


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/97157990-be0a-39f4-c7d2-a3595b8e2592%40cafu.de.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/21066F2F-1E4C-4293-94CC-3A6F512F3353%40uniquode.io.
Reply | Threaded
Open this post in threaded view
|

Re: Tests: How to messages.add_message() before self.client.get() ?

Carsten Fuchs-4
Thank you very much!
I'll try that!

Best regards,
Carsten


Am 11.11.20 um 02:28 schrieb David Nugent:

> Make your own request.
>
> I don't see the need here but incase I'm missing something: request is just a HTTPRequest <https://docs.djangoproject.com/en/3.1/ref/request-response/#httprequest-objects> instance. You can instantiate it like any other python class. The only downside is that the request you manufacture won't have middleware applied or in fact anything else that is populated and processed before it gets to your handler (headers, method, parsed url parts, content and so on).
>
> This may not actually be an issue in test scenarios, depending on your need, and in this case on the backing store <https://docs.djangoproject.com/en/3.1/ref/contrib/messages/#storage-backends> used by the messages framework.  If it is set to session, then you'll need to obtain a session from the client and stick it onto your request object if you want that to persist through your subsequent test requests.
>
> HTH /d
>
>> On 10 Nov 2020, at 18:41, Carsten Fuchs <[hidden email] <mailto:[hidden email]>> wrote:
>>
>> Dear group,
>>
>> in my tests I would like to test the processing of messages in the view.
>>
>> For example:
>>
>>    def test_is_message_properly_handled(self):
>>        # The problem: We have no `request` here!
>>        messages.info <http://messages.info>(request, 'Hello')
>>        response = self.client.get("/handling-messages-view/")
>>        # asserts …
>>
>> This is an artificial example and I'm aware that I could submit a POST request and then use `follow=True` so that eventually the messages are processed. But I would like to setup messages and their processing independently as shown above.
>> The problem is that we don't have the `request` instance that is used by the test client.
>> Is this possible?
>>
>> Best regards,
>> Carsten
>>
>>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1f9785eb-089f-491c-d9f5-2682f2e0a5ff%40cafu.de.