Are there any docs or guides on how to write automated tests? #2125
-
Hey there, As an example I think this question describes "my struggles" a little bit but it would be nice if there would be a general "best practises for testing" section :) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Only solution to properly unit test this lib is to go with the last suggestion in you stackoverflow post. Wrap the calls from the DiscordSocketClient in your Interface methods. You can then mock your Interface in your tests. |
Beta Was this translation helpful? Give feedback.
-
@MichaelHochriegl thanks. So yes, I could create something like this public interface IDiscordClient
{
Task CreateSession();
Task DestroySession();
}
public sealed class DiscordClient : IDiscordClient
{
private readonly DiscordSocketClient _discordSocketClient;
public DiscordClient(DiscordSocketClient discordSocketClient)
{
_discordSocketClient = discordSocketClient;
}
public async Task CreateSession()
{
await _discordSocketClient.LoginAsync(TokenType.Bot, "read token from injected options");
await _discordSocketClient.StartAsync();
}
public async Task DestroySession()
{
await _discordSocketClient.LogoutAsync();
await _discordSocketClient.StopAsync();
}
} but I'm struggling with 2 things here
Any suggestions? |
Beta Was this translation helpful? Give feedback.
-
If you want to test the actual DiscordClient you would need to do Integration Tests. Unit tests mock dependencies away. The DiscordClient is your dependency here, so for unit testing you make this mockable with the code you posted above. You don't reimplement this mocked methods, you ideally use a framework like NSubstitute or Moq to fake the return of said call. |
Beta Was this translation helpful? Give feedback.
Only solution to properly unit test this lib is to go with the last suggestion in you stackoverflow post.
So you have to create an Interface with an Implementation that uses an injected DiscordSocketClient to it.
Wrap the calls from the DiscordSocketClient in your Interface methods. You can then mock your Interface in your tests.