【问题标题】:Django Channels doesn't run basic test caseDjango Channels 不运行基本测试用例
【发布时间】:2017-08-26 02:48:34
【问题描述】:

我正在尝试使用 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,
 }

这给了我以下错误:

错误:test_ros_channel (robot_configuration_interface.tests.unit.test_channels.ROSChannelTestCase) -------------------------------------------------- -------------------- Traceback(最近一次调用最后一次):文件 “/home/cjds/development/robot_configuration_interface/robot_configuration_interface/tests/unit/test_channels.py”,第 36 行,在 test_ros_channel client.send_and_consume('websocket.connect', '/new/') 文件 "/usr/local/lib/python2.7/dist-packages/channels/test/http.py", 行 94,在 send_and_consume self.send(channel, content, text, path) 文件“/usr/local/lib/python2.7/dist-packages/channels/test/http.py”,行 79、在发送 content.setdefault('reply_channel', self.reply_channel) AttributeError: 'str' object has no attribute 'setdefault'

我正在尝试在这里学习本教程:

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

【问题讨论】:

    标签: python django unit-testing django-channels


    【解决方案1】:

    频道 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 评论中所述。

    【讨论】:

    • 暂时把它标记下来,因为答案在很多方面都是错误的。例如。 content='/new/' 不正确,但 path='/new/' 是正确的。感谢您的努力,如果您可以将github.com/django/channels/issues/586 合并到您的答案中并删除错误,我会正确标记它
    • 好吧,如果您能阅读错误呼叫周围的文字,您就会明白您抱怨的那一行,尤其是 content='/new'your 代码行,并且 -当然 - 这是错误的,因为 content 必须是字典,正如我在上面的回答中已经说过的那样。
    • @cjds:您应该重新考虑答案中写的内容,这对我来说是正确的!
    • 谢谢@AmirHadi 我没有看到编辑。标记正确
    • 此问题和答案适用于渠道 1.x。对于 2.x,请参阅 channels.readthedocs.io/en/latest/topics/testing.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    相关资源
    最近更新 更多