简体   繁体   English

如何为 discord 机器人编写单元测试

[英]How to write unit-tests for discord bot

I'm currently tried to write my first project with TDD and I've chosen a discord bot based on Discord.Net to do so.我目前正在尝试使用 TDD 编写我的第一个项目,并且我选择了一个基于 Discord.Net 的 discord 机器人来执行此操作。

But right off the start I face a problem regarding the abstract classes that Discord.Net uses.但是一开始我就面临一个关于 Discord.Net 使用的抽象类的问题。 My first tests should concern the CommandHandler (which I will later extract into it's Interface and Implementation part).我的第一个测试应该关注 CommandHandler(稍后我将提取到它的接口和实现部分)。

The first test should check that the command is ignored if the bot is the author of the command.如果机器人是命令的作者,第一个测试应该检查命令是否被忽略。 Therefore my OnMessageReceivedAsync -method will accept a SocketMessage , but this is an abstract class.因此,我的OnMessageReceivedAsync方法将接受SocketMessage ,但这是一个抽象的 class。

How should I go about this and write proper unit tests for an Discord.Net based discord bot?我应该如何 go 关于这个并为基于 Discord.Net 的 discord 机器人编写适当的单元测试?

UPDATE: I even tried it with Moq and mocking an IMessage , but I can't setup the mock because pretty much all of the fields are readonly.更新:我什至尝试使用 Moq 和 mocking 和IMessage ,但我无法设置模拟,因为几乎所有字段都是只读的。 So I can't set the Author with this either.所以我也不能用这个设置Author

My mock try is as follows:我的模拟尝试如下:

using Discord;
using Moq;
using Xunit;

namespace Runa.DiscordBot.Infrastructure.Tests
{
    public class CommandHandlerTests
    {
        [Fact]
        public void OnMessageReceivedAsync_IgnoresSelfAsCommandIssuer()
        {
            Mock mockMessage = new Mock<IMessage>()
                .Setup(msg => msg.Author = "bot");
        }
    }
}

But as stated above with this I get the access-error when I try to set the msg.Author = "bot" , because the Author field has no set accessor.但如上所述,当我尝试设置msg.Author = "bot"时出现访问错误,因为Author字段没有set访问器。

Mock<IMessage> mockMessage = new Mock<IMessage>();

mockMessage.SetupGet(msg => msg.Author).Returns("bot");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM