unit-testing - GoLang Sarama ConsumerGroup模拟

标签 unit-testing go mocking sarama testify

我是Go的新手,我也在努力模拟通话:sarama.NewConsumerGroup(brokers, group, config)
我正在使用testify,我的模拟代码当前如下所示:

type MyMockedObjectReciever struct {
    mock.Mock
    Receiver
}

func (m *MyMockedObjectReciever) mockCreateConsumer(brokers []string, group string, config *sarama.Config) (sarama.ConsumerGroup, error) {
    args := m.Called(brokers, group, config)
    return args.Get(0).(sarama.ConsumerGroup), args.Error(1)
}

// mock connection and subscribe
        wantConsumer := sarama.NewConsumerGroup
        createConsumer = c.mockCreateConsumer
        c.On("mockCreateConsumer", []string{testBrokers}, testGroup, wantConfig).Return(wantConsumer, nil).Once()

但是我得到了错误:
--- FAIL: TestKafkaReceiver (0.00s)
    --- FAIL: TestKafkaReceiver/test_a_Kafka_receiver (0.00s)
panic: interface conversion: func([]string, string, *sarama.Config) (sarama.ConsumerGroup, error) is not sarama.ConsumerGroup: missing method Close [recovered]
    panic: interface conversion: func([]string, string, *sarama.Config) (sarama.ConsumerGroup, error) is not sarama.ConsumerGroup: missing method Close

我相信我打错了电话,但现在确定还有什么要做的。

最佳答案

您可以像下面这样编写您的模拟消费者,它将满足消费者组对象。

// Consumergroup handler
type testConsumerGroupHandler struct {
}

func (C testConsumerGroupHandler) Consume(ctx context.Context, topics []string, handler sarama.ConsumerGroupHandler) error {
    return nil
}
func (C testConsumerGroupHandler) Errors() <-chan error {
    return nil
}
func (C testConsumerGroupHandler) Close() error {
    return nil
}

之后,您应该编写成功和失败的方法,并相应地设置模拟测试

关于unit-testing - GoLang Sarama ConsumerGroup模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62391830/

相关文章:

html - 使用 Karma 生成 HTML 测试报告

ios - Xcode 单元测试和写入数据到磁盘

node.js - 当客户端发出时如何测试 socket.io 服务器

mongodb - Golang mgo 结果变成简单的 slice

c# - 单元测试在 System.Net.Http v4 中使用 HttpClient 的类

.net - 如何使用 .NET 代码以自动化方式运行 Microsoft Visual Studio 单元测试?

json - 解析嵌套的 json 对象 golang

go - 在Go工作区中安装Gota软件包

python - 如何模拟模块python,补丁找不到属性

java - 对调用外部服务的类进行单元测试