我是 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我相信我錯誤地嘲笑了電話,但現(xiàn)在確定還能做什么。
1 回答

神不在的星期二
TA貢獻1963條經(jīng)驗 獲得超6個贊
您可以像下面這樣編寫您的模擬消費者,這將滿足消費者組對象。
// 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
}
之后,您應該編寫成功和失敗的方法,并相應地設置模擬測試
- 1 回答
- 0 關(guān)注
- 343 瀏覽
添加回答
舉報
0/150
提交
取消