From 7de6ff17d6ec0ce10df323a4358573bac96b60c7 Mon Sep 17 00:00:00 2001 From: Sam Bolgert Date: Sat, 27 Aug 2016 13:08:31 -0700 Subject: [PATCH] Fixed #251: Add docs for testing Generic Consumers (#323) --- docs/testing.rst | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/testing.rst b/docs/testing.rst index 73b250e..11a5aa3 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -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 ------