Reputation: 46
So I'm using django_channels
to handle some WebSocket stuff and since Django 3.1 you can create unittest-like
tests for testing async
and decided to go with that.
It happens to be that for some reason when accessing the Consumer
can't reach the data.
I'm using model_bakery
(but also tried with plain Django ORM) and have a very simple test.
class TestChatConsumer(TestCase):
url = '/ws/chat/'
def setUp(self):
self.user = baker.make_recipe('registration.user')
self.chat = baker.make_recipe('chat.chat')
async def test_setup_channel_layer_ok(self):
consummer = WebsocketCommunicator(
application=AuthMiddlewareStack(ChatConsumer.as_asgi()),
path=self.url,
)
consummer.scope['user'] = self.user
await consummer.connect()
await consummer.send_json_to({
'type': 'setup_channel_layer',
'chat': self.chat.pk,
})
response = await consummer.receive_json_from()
self.assertEqual(response['type'], 'info')
self.assertEqual(response['content']['message'], 'Chat connected!')
The problem is that on the test the entry is created but when accessing consumers the entry seems to be off. Do you know if there's any desync between test database or something?
I added a dummy function to check for tests on consumer and got this.
Upvotes: 1
Views: 3560
Reputation: 46
So at the end the problem was django.test.TestCase
.
It was fixed by changing class TestChatConsumer(TestCase):
for class TestChatConsumer(TransactionTestCase):
Seems like TransactionTestCase
comes with a lot of features that TestCase
doesn't have.
Upvotes: 2