1

I'm trying to test my consumers with the testing framework from django channels, but even a basic test doesn't seem to work

This is what my test case looks like:

from channels import Channel
from channels.test import ChannelTestCase, HttpClient, apply_routes
from rci.consumers import Demultiplexer
from rosbridge.consumers import OtherWebSocketConsumer

class ChannelTestCase(ChannelTestCase):

    def test_channel(self):
        client = HttpClient()
        client.send_and_consume('websocket.connect', '/new/') # <--- here's the error
        self.assertIsNone(client.receive())

This is my routing:

http_routing = [
    route("http.request", admin.site.urls, path=r"^/admin/", method=r"^$"),
    #...and so on
]

channel_routing = [Demultiplexer.as_route(path=r"^/sock/")]

This is my consumer:

class Demultiplexer(WebsocketDemultiplexer):
    channel_session_user = True

   consumers = {
       "rosbridge": ROSWebSocketConsumer,
       "setting": SettingsConsumer,
 }

This gives me the following error:

ERROR: test_ros_channel (robot_configuration_interface.tests.unit.test_channels.ROSChannelTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/cjds/development/robot_configuration_interface/robot_configuration_interface/tests/unit/test_channels.py", line 36, in test_ros_channel client.send_and_consume('websocket.connect', '/new/') File "/usr/local/lib/python2.7/dist-packages/channels/test/http.py", line 94, in send_and_consume self.send(channel, content, text, path) File "/usr/local/lib/python2.7/dist-packages/channels/test/http.py", line 79, in send content.setdefault('reply_channel', self.reply_channel) AttributeError: 'str' object has no attribute 'setdefault'

I'm trying to follow this tutorial here:

http://channels.readthedocs.io/en/stable/testing.html#clients

4

1 回答 1

3

频道 1.x

send_and_consume使用两个位置参数进行调用,这会导致此调用生效(这正是在此行执行期间发生错误的原因):

# AGAIN this is wrong code this is what is written in the question
# only difference is the naming of the (previously positional) arguments
client.send_and_consume(channel='websocket.connect', content='/new/')

这是错误原因的解释:

但是,send_and_consume 期望的实现content是一个字典

def send_and_consume(self, channel, content={}, text=None, path='/', fail_on_none=True, check_accept=True):
        """
        Reproduce full life cycle of the message
        """
        self.send(channel, content, text, path)
        return self.consume(channel, fail_on_none=fail_on_none, check_accept=check_accept)

实现代码取自:https ://github.com/django/channels/blob/master/channels/test/http.py#L92

频道 2.x

请参阅https://channels.readthedocs.io/en/latest/topics/testing.html,如 Paul Whipp 的评论中所述。

于 2017-03-31T18:46:30.023 回答