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 ------