我一直在尝试为频道的多聊天示例项目中的一位消费者创建测试:https ://github.com/andrewgodwin/channels-examples/tree/master/multichat 。消费者函数如下所示:
@channel_session_user
def chat_join(message):
print(message.content)
room = get_room_or_error(message[MsgFields.ROOM], message.user)
if NOTIFY_USERS_ON_ENTER_OR_LEAVE_ROOMS:
room.send_msg(None, message.user.nickname, message.user.id, MSG_TYPE_ENTER)
room.websocket_group.add(message.reply_channel)
message.channel_session[ChannelSession.ROOMS] = list(set(message.channel_session[ChannelSession.ROOMS]).union([room.id]))
recent_msgs = Message.objects.filter(room=room.id).order_by(ChatModelFields.CREATED_AT)[:10]
history = []
for msg in recent_msgs:
history.append({
ChatModelFields.CONTENT: msg.content,
ChatModelFields.CREATOR: msg.creator,
MsgFields.MSG_TYPE: MSG_TYPE_MESSAGE,
})
message.reply_channel.send({
'text': json.dumps({
MsgFields.USER_JOIN: str(room.id),
'history': history,
})
})
现在,我一直遇到的有趣错误是这样的。在我的测试功能版本 1 中:
def test_chat_send(self):
user = HubUser.objects.create_user(
self.TEST_EMAIL,
password=self.TEST_PASSWORD,
nickname=self.TEST_NICKNAME
)
room = ChatRoom.objects.create(creator=self.TEST_NICKNAME)
msg_join = {
MsgFields.ROOM: 1,
MsgFields.COMMAND: 'join',
}
msg_send = {
MsgFields.MESSAGE: self.TEST_MSG,
MsgFields.CREATOR: user,
MsgFields.ROOM: 1,
MsgFields.COMMAND: 'send',
}
c = HttpClient()
c.login(username=self.TEST_EMAIL, password=self.TEST_PASSWORD)
c.send_and_consume(RECEIVER_CHANNEL, content=msg_join, fail_on_none=True)
测试给了我以下错误:
Traceback (most recent call last):
File "J:\Web\HubService\src\chat\tests\tests.py", line 60, in test_chat_send
c.send_and_consume(RECEIVER_CHANNEL, content=msg_join, fail_on_none=True)
File "C:\Users\Doge\Envs\dogeenv\lib\site-packages\channels\tests\base.py", line 127, in send_and_consume
return self.consume(channel, fail_on_none=fail_on_none)
File "C:\Users\Doge\Envs\dogeenv\lib\site-packages\channels\tests\base.py", line 118, in consume
raise AssertionError("Can't find consumer for message %s" % message)
AssertionError: Can't find consumer for message <channels.message.Message object at 0x0000000006086B70>
测试功能版本 2:
def test_chat_send(self):
user = HubUser.objects.create_user(
self.TEST_EMAIL,
password=self.TEST_PASSWORD,
nickname=self.TEST_NICKNAME
)
room = ChatRoom.objects.create(creator=self.TEST_NICKNAME)
msg_join = {
MsgFields.ROOM: 1,
MsgFields.COMMAND: 'join',
}
msg_send = {
MsgFields.MESSAGE: self.TEST_MSG,
MsgFields.CREATOR: user,
MsgFields.ROOM: 1,
MsgFields.COMMAND: 'send',
}
c = HttpClient()
c.login(username=self.TEST_EMAIL, password=self.TEST_PASSWORD)
c.send(RECEIVER_CHANNEL, content=msg_join)
c.consume(RECEIVER_CHANNEL, fail_on_none=True)
测试现在可以成功地将消息路由到正确的消费者,但由于用户身份验证而无法继续:
Traceback (most recent call last):
File "J:\Web\HubService\src\chat\tests\tests.py", line 59, in test_chat_send
c.consume(RECEIVER_CHANNEL, fail_on_none=True)
File "C:\Users\Doge\Envs\dogeenv\lib\site-packages\channels\tests\base.py", line 116, in consume
return consumer(message, **kwargs)
File "C:\Users\Doge\Envs\dogeenv\lib\site-packages\channels\sessions.py", line 64, in inner
return func(message, *args, **kwargs)
File "C:\Users\Doge\Envs\dogeenv\lib\site-packages\channels\auth.py", line 42, in inner
return func(message, *args, **kwargs)
File "J:\Web\HubService\src\chat\consumers.py", line 38, in chat_join
room = get_room_or_error(message[MsgFields.ROOM], message.user)
File "J:\Web\HubService\src\chat\utils.py", line 24, in get_room_or_error
raise ClientError("User not logged in")
chat.exceptions.ClientError: User not logged in
我的问题是:
1) 为什么调用HttpClient.send
和HttpClient.consume
分别产生与调用不同的行为HttpClient.send_and_consume
。我查看了 send_and_consume 函数,它只是像我做的那样分别调用发送和消费。
2)我怎样才能让它工作,这样我就不会得到用户登录错误?