Django Channels 没有 运行 基本测试用例

Django Channels doesn't run basic test case

我正在尝试使用来自 django 频道的测试框架来测试我的消费者,但即使是基本测试似乎也不起作用

这是我的测试用例的样子:

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

这是我的路线:

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

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

这是我的消费者:

class Demultiplexer(WebsocketDemultiplexer):
    channel_session_user = True

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

这给了我以下错误:

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'

我正在尝试按照此处的教程进行操作:

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

频道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 在评论中提到的那样。