0

我是 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

我相信我错误地嘲笑了电话,但现在确定还能做什么。

4

1 回答 1

1

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

// 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
}

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

于 2020-06-18T00:45:04.567 回答