I'm using the Discord.Net package offering me the DiscordSocketClient class, which currently is a Singleton in my DI container. I need several methods from the instance
Task LoginAsync(TokenType tokenType, string token, bool validateToken = true)Task StartAsync()Task LogoutAsync()Task StopAsync()
Given this example class consuming the methods
public sealed class ConsumingClass
{
private readonly DiscordSocketClient _discordSocketClient;
public ConsumingClass(DiscordSocketClient discordSocketClient)
{
_discordSocketClient = discordSocketClient;
}
public override async Task Initialize(CancellationToken cancellationToken)
{
await _discordSocketClient.LoginAsync(TokenType.Bot, "my-token");
await _discordSocketClient.StartAsync();
}
public override async Task TearDown(CancellationToken cancellationToken)
{
await _discordSocketClient.LogoutAsync();
await _discordSocketClient.StopAsync();
}
}
I want to create tests using XUnit and Moq to ensure the instance of ConsumingClass works as expected.
Unfortunately DiscordSocketClient does not offer an interface so I can't inject a mock implementation to my tests.
Also the methods are not virtual, so it is not possible to create an instance of Mock<DiscordSocketClient> and use .Setup() to setup mock implementations.
So how can I verify in my testcase, that the client methods have been called once? E.g. _discordSocketClientMock.Verify(discordSocketClient => discordSocketClient.LoginAsync(), Times.Once());
Do I have to create an interface IMyDiscordSocketClient providing those methods and a class another class MyDiscordSocketClient inheriting from DiscordSocketClient and implementing the interface and use that? Or are there any better ways?