I am new to Go and I am struggling to mock out the call too: sarama.NewConsumerGroup(brokers, group, config)
I am using testify and my mocked code currently looks like:
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()
But I get the error:
--- 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
I beleive I am mocking the call incorrectly but now sure what else to do.
You could write your mock consumer like this below which will satisfy the consumergroup object.
// 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
}
Afterwards, you should write your methods for success and failure and accordingly setup mock testing