Fixed #251: Add docs for testing Generic Consumers (#323)

This commit is contained in:
Sam Bolgert 2016-08-27 13:08:31 -07:00 committed by Andrew Godwin
parent e6236f79e6
commit 7de6ff17d6

View File

@ -62,6 +62,31 @@ and post the square of it to the ``"result"`` channel::
self.assertEqual(result['value'], 1089)
Generic Consumers
-----------------
You can use ``ChannelTestCase`` to test generic consumers as well. Just pass the message
object from ``get_next_message`` to the constructor of the class. To test replies to a specific channel,
use the ``reply_channel`` property on the ``Message`` object. For example::
from channels import Channel
from channels.tests import ChannelTestCase
from myapp.consumers import MyConsumer
class MyTests(ChannelTestCase):
def test_a_thing(self):
# Inject a message onto the channel to use in a consumer
Channel("input").send({"value": 33})
# Run the consumer with the new Message object
message = self.get_next_message("input", require=True)
MyConsumer(message)
# Verify there's a reply and that it's accurate
result = self.get_next_message(message.reply_channel.name, require=True)
self.assertEqual(result['value'], 1089)
Groups
------