from unittest.mock import AsyncMock import pytest from discord.ext import commands from bot import Music @pytest.mark.asyncio async def test_bot_ensure_voice(bot, ctx): mbot = Music(bot) # 1. Inside a voice channel # 1.1 Does not call stop if no sound is playing ctx.voice_client.is_playing.return_value = False await mbot.ensure_voice(ctx) assert ctx.voice_client.stop.call_count == 0 ctx.reset_mock(return_value=True) # 1.2 Does call stop if sound is playing ctx.voice_client.is_playing.return_value = True await mbot.ensure_voice(ctx) assert ctx.voice_client.stop.call_count == 1 ctx.reset_mock(return_value=True) # 2. Not inside a voice channel # 2.1 Connects to voice channel of author if possible ctx.voice_client = None ctx.author.voice = AsyncMock() await mbot.ensure_voice(ctx) assert ctx.author.voice.channel.connect.call_count == 1 ctx.reset_mock(return_value=True) # 2.2 Error if author not inside a channel ctx.voice_client = None ctx.author.voice = None with pytest.raises(commands.CommandError): await mbot.ensure_voice(ctx)